LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags ,

Reply
 
Thread Tools Search this Thread
Old 05-23-2009, 03:38 PM   #1
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Gentoo/Debian/Kubuntu
Posts: 353
Thanked: 31
php XMLReader duplicate entry... Bug?


[Log in to get rid of this advertisement]
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

Last edited by eco; 05-24-2009 at 02:55 AM.. Reason: changed XML to make it more clear
eco is offline  
Tag This Post ,
Reply With Quote
Old 05-23-2009, 08:12 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 11
Posts: 1,899
Thanked: 31
You have an error with the xml. The open tag is Users the close tag is User
graemef is offline     Reply With Quote
Old 05-24-2009, 02:57 AM   #3
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Gentoo/Debian/Kubuntu
Posts: 353
Thanked: 31

Original Poster
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.
eco is offline     Reply With Quote
Old 05-24-2009, 05:35 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 11
Posts: 1,899
Thanked: 31
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.
graemef is offline     Reply With Quote
Old 05-24-2009, 08:03 AM   #5
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Gentoo/Debian/Kubuntu
Posts: 353
Thanked: 31

Original Poster
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");
            }
        }
    }
}
?>
eco is offline     Reply With Quote
Old 05-24-2009, 09:22 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 11
Posts: 1,899
Thanked: 31
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).
graemef is offline     Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP not starting - xmlreader module problem riamu Fedora 0 08-15-2008 08:44 AM
duplicate entry finding cmontr Linux - General 1 05-22-2008 12:04 AM
PHP XMLReader stops at bad character MicahCarrick Programming 3 06-07-2007 08:25 PM
LXer: Memory-efficient XML parsing in PHP with XMLReader LXer Syndicated Linux News 0 02-04-2007 06:33 PM
Im getting a mysql_error Duplicate entry? blanderson Linux - Newbie 0 10-17-2003 01:29 PM


All times are GMT -5. The time now is 06:00 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration