Reputation: 866
Let's say I have this XML file.
<book>
<id>1</id>
<title>Harry Potter - bla bla bla</title>
<author>J.K Rowling</author>
</book>
<book>
<id>2</id>
<title>Other book</title>
<author>A Name</author>
</book>
Is there a way where I can read via PHP and get the #2 id, or do I have to use an IF?
Like jQuery selector ':eq(2)', or MySql 'WHERE id
=2'
Upvotes: 3
Views: 683
Reputation: 1672
If all you want is just the second one you can use DOM. It's simpler.
$dom->loadXML(<<<XML
<book>
<id>1</id>
<title>Harry Potter - bla bla bla</title>
<author>J.K Rowling</author>
</book>
<book>
<id>2</id>
<title>Other book</title>
<author>A Name</author>
</book>
XML;);
$book=$dom->getElementsByTagName('book')->item(1);
Edit: I just saw you say you were looking for second ID, not second element, you need xpath for that.
$xml=new SimpleXMLElement(<<<XML
<book>
<id>1</id>
<title>Harry Potter - bla bla bla</title>
<author>J.K Rowling</author>
</book>
<book>
<id>2</id>
<title>Other book</title>
<author>A Name</author>
</book>
XML;);
$result=$xml->xpath('/book[id=2]');
More on xpath here
Upvotes: 2
Reputation: 2200
There is, try SimpleXML parser of php: http://php.net/manual/en/book.simplexml.php
Upvotes: 3