php XMLReader duplicate entry... Bug?
Hi all,
Should I be using another Class to parse my XML? I've been trying to use XMLReader to get the info I need from the XML but it seems to duplicate the result. I suspect it finds <User ...> and finds the attribute within... then gets to </User> and looks for the attribute again! Here is a sample of the XML Code:
<?xml version="1.0"?> PHP Code:
The output Code:
0 WRONG Local Name: Users Can you help me get rid of the duplicate result? Thanks |
You have an error with the xml. The open tag is Users the close tag is User
|
Hi graemef,
Thanks for the reply but that's not it though. ;) I was showing a section of the XML... Now that I've added three dots and closed the XML with </Users>, do you have a clue? ;) Thanks for any help or advice on how to do this another way without rewritting a Class to do it. |
I'm not 100% certain about how the library works but in general xml parser libraries will read in each tag, you will then need to decide what to do with that tag. Your second "User: jdoe" is when you reach the user close tag, because you don't initialise $login within the loop it still has a value.
|
What you said confirmed my suspicions so I went back to the class and looked for a way to deal whith it.
Here is what I came up with. I hope it can help someone else some day :) PHP Code:
|
Looking at the manual you can use the $nodeType property to determine if it is an open (XMLReader::ELEMENT) or close tag (XMLReader::END_ELEMENT).
|
Xml reader problem
I'm using xmlreader to load an xml file like:
<head> <element param="hello"/> <element param="hello"/> <element param="hello"/> </head> And all is OK! But now i need to write many times the xml and append elements... I have a problem with the <head></head>!!!!!!!!!!!! without <head></head> should be perfect, i could simply append using php functions: <element param="hello"/> <element param="hello"/> <element param="hello"/> <element param="hello"/> <element param="hello"/> ... But without <head></head> xmlreader give error at opening!! How can i read using xmlreader an xml <head></head>??? or How can i write an xml between the last element and the </head> ??? Please help |
First this thread is over a year old. It would have been better to have started a new thread.
XMLReader is for reading and you appear to be talking about writing. look at XMLWriter and the documentation for the methods: XMLWriter::startElement() XMLWriter::endElement() XMLWriter::writeElement() XMLWriter::writeAttribute() |
All times are GMT -5. The time now is 06:30 PM. |