LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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

Reply
 
Search this Thread
Old 05-23-2009, 02:38 PM   #1
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
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

Last edited by eco; 05-24-2009 at 01:55 AM. Reason: changed XML to make it more clear
 
Old 05-23-2009, 07:12 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You have an error with the xml. The open tag is Users the close tag is User
 
Old 05-24-2009, 01:57 AM   #3
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Original Poster
Rep: Reputation: 48
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.
 
Old 05-24-2009, 04:35 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 05-24-2009, 07:03 AM   #5
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Original Poster
Rep: Reputation: 48
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");
            }
        }
    }
}
?>
 
Old 05-24-2009, 08:22 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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).
 
Old 10-23-2010, 01:08 PM   #7
raccia
LQ Newbie
 
Registered: Oct 2010
Posts: 1

Rep: Reputation: 0
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
 
Old 10-23-2010, 08:42 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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()
 
  


Reply

Tags
php


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-13-2008 10:51 PM
duplicate entry finding cmontr Linux - General 1 05-21-2008 11:04 PM
PHP XMLReader stops at bad character MicahCarrick Programming 3 06-07-2007 07:25 PM
LXer: Memory-efficient XML parsing in PHP with XMLReader LXer Syndicated Linux News 0 02-04-2007 05:33 PM
Im getting a mysql_error Duplicate entry? blanderson Linux - Newbie 0 10-17-2003 12:29 PM


All times are GMT -5. The time now is 10:43 AM.

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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration