LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   php XMLReader duplicate entry... Bug? (http://www.linuxquestions.org/questions/programming-9/php-xmlreader-duplicate-entry-bug-728048/)

eco 05-23-2009 02:38 PM

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"?>
<Users>
  <User LoginName="jdoe" Alias="John Doe">
  <Contact Name="John Doe" Email="jdoe@home.com"/>
  </User>
  ...
</Users>

Here is a sample of my php script to extract the LoginName
PHP Code:

Class Parse
{
    public function 
ListAllUserLogins($xml)
    {
        
$reader = new XMLReader();
        
$reader->XML($xml);

        
$i=0;
        While (
$reader->read())
        {
            echo 
"$i  ";
            if (
$reader->localName == "User")
            {
                echo 
"$reader->localName: ";
                
$login $reader->getAttribute("LoginName");
                if (
$login != "")
                {
                    echo 
"$login\n";
                } else {
                    echo 
"EMPTY\n";
                }
            } else {
                echo 
"WRONG Local Name: $reader->localName\n";
            }
            
$i++;
        }
        
$reader->close();
    }


  • The echo are to debug...
  • Any improvements to the above are welcome... I'm a newb ;)

The output
Code:

0  WRONG Local Name: Users                                                                                                                                                                   
1  User: jdoe                                                                                                                                                                         
2  WRONG Local Name: Contact                                                                                                                                                                 
3  User: jdoe

jdoe is found twice! :(

Can you help me get rid of the duplicate result?

Thanks

graemef 05-23-2009 07:12 PM

You have an error with the xml. The open tag is Users the close tag is User

eco 05-24-2009 01:57 AM

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.

graemef 05-24-2009 04:35 AM

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.

eco 05-24-2009 07:03 AM

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:

<?php

Class Parse
{
    public function 
ListAllUserLogins($xml)
    {
        
$reader = new XMLReader();
        
$reader->XML($xml);

        While (
$reader->read())
        {
            if (
$reader->localName == "User" && $reader->getAttribute("LoginName") != "")
            {
                
$ln $reader->getAttribute("LoginName");
                
$reader->next("User");
            }
        }
    }
}
?>


graemef 05-24-2009 08:22 AM

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).

raccia 10-23-2010 01:08 PM

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

graemef 10-23-2010 08:42 PM

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:52 AM.