|
Posted by Disguised Jedi on 05/03/05 17:52
> I have an xml document storing some data I need. What I want to do is this:
> 1. Scan to the end of the file.
> 2. Find the closing tag.
> 3. Insert a new entry in before the closing tag.
There are specific classes and functions in the PHP core that can help
you do just this.
> I've tried:
> 1. Creating new files and renaming them to be the original.
> 2. Writing the file to a dummy file and insert my lines part way through
> then finish the last tag.
I don't think this is the right approach to take. Maybe using the DOM
and/or the XML Parser built-in to php would help you.
> My problem is I'm looking for a </endtag> and it comes up as endtag. Is
> there anyway to force PHP to read the .xml file as a text file so it wont
> strip off the xml tag information?
>
> I've used fopen with fgets and fwrite, and file with fwrite
Again, this isn't the approach to take. There are built-in functions
for this sort of thing, as well as classes on PEAR that will help you.
Try google!
Read through all of this and decide which approach you want to take
before you start anything...
If you use Windows or compile PHP as an Apache module in Linux, you're
all set with the XML Parser. If not, you'll need to get the library
according to the directions in the manual.
http://www.php.net/xml
Read everything in that thoroughly, and understand it before you
continue with this approach. Copy and play with the examples to see
how it all works. If you'd rather handle things in an environment
designed to create and manipulate XML dynamically, rather than just
read and insert raw strings, keep reading.
You can get the DOM XML working easily in Windows, and even easier in
a GNOME environment on a linux box. The manual has excellent
instructions.
http://www.php.net/domxml
This extension is Object-Oriented (OO), and if you don't know what
that is, you better read up on it before you try this out. If you do,
and have an OK understanding of how OO works in PHP, then read on! If
not, read the article....
http://www.php.net/oop
Now, understand that DOM XML is very particular about the syntax and
such in an XML file. The file MUST start with the XML declaration
(without quotes) "<?xml version=1.0>" in order for DOM to try and use
it.
Use the functions to read right through it all. You'll find that this
is probably exactly what you need!
I'd use DOM XML personally. I included the other in case you didn't
want to have to install everything.
Have fun, and good luck with PHP!
--
disguised.jedi@gmail.com
PHP rocks!
"Knowledge is Power. Power Corrupts. Go to school, become evil"
Disclaimer: Any disclaimer attached to this message may be ignored.
However, I must say that the ENTIRE contents of this message are
subject to other's criticism, corrections, and speculations.
This message is Certified Virus Free
[Back to original message]
|