XML and Php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

XML and Php

Post by user___ »

Hi guys,
I asked this a short time ago but I got no answer. I hope that now there would be someone who could help.

Well, I have a .xml file which has a root tag and subtags. In each subtag there is an id. I need to get the last id of a subtag in the list.
This is a sample of my .xml

Code: Select all

...
<root>
   <list id="1" />
   <list id="2" />
   <list id="3" />
</root>
...
I need to get three as a value from my Php script.

I would like to know whether this is possible with Php(and how if it is) or not.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

What's your PHP version?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

A lot depends on how large and complex your XML is possibly going to be. Options if small include things like preg_match_all. If larger/more complex than your example you may wish to look at actually processing your XML using something like SimpleXML or one of the other PHP XML libraries such as XML Parser Functions. The main trouble with XML parsing is to ensure that the original data is well formed, even if it not valid (matched to a specific Document Type Definition (DTD)).

Hope that helps

Coder
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post by user___ »

Oren: I use Php 5.
CoderGoblin:Thank you for the reply. My XML is simple in structure the same as I posted and there can be one more child in it. The content may become a lot(list tags can be hundreds).
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

The next question you need to ask then is how am I getting this information in. Is it line by line or is a string. If you are reading line by line then it should be easy to use simply keep a running tag on every occurance of the list. If it is a string already I would use preg_match (personal preference). If the lists are always running from 1-x seqentially all you would need to do is count the occurances of the <list> tags. If not you need to work out a pattern to extract the last list attribute.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Reply

Post by user___ »

Thank you, CoderGoblin for your reply. Actually, I need to get the number of ids but although you explained me how to do it very well, I would appreciate it if you could post me a sample code on how to do it.

BTW:Lines are running sequentially.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

user___ wrote:BTW:Lines are running sequentially.
Then you can use the xpath 1.0 function last()

Code: Select all

$xml = <<< eox
<root>
   <list id="1" />
   <list id="2" />
   <list id="3" />
</root>
eox;


$dom = new DOMDocument::loadXML($xml);

$xpath = new DOMXPath($dom);
$nodeset = $xpath->query('//list[last()]/@id');
echo $nodeset->item(0)->nodeValue;
Post Reply