SimpleXmlElement + XPath. Parental node index
-
Good day, respected programmers. There's a xml file:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book><book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book><book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book><book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book></bookstore>
With XPath, I find the title of the right book.
$xml->xpath("//title[text() = 'Harry Potter']);
And I'm facing a problem finding an index for a parent knot.
<book>
For example: For a title book"Harry Potter"
Index1
♪
Please tell me how to get the book index. Later, thank you so much.
-
Use the function simple_xml_load_string() or simple_xml_load_file() and this problem will be solved.
$string=<<<XML <?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book><book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book><book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book><book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book></bookstore>
XML;$xml = simplexml_load_string($string);
//print $xml->book[1]->title;
$cnt = count($xml);
for($i=0; $i<$cnt-1; $i++){
$title = $xml->book[$i]->title;
print "<br> index = $i title =$title";
if($title == 'Harry Potter') $index = $i;
}
print("<br><br>index_required = $index");print("<pre>"); print_r($xml); print("</pre>");
Results:
index = 0 title = Everyday Italian
index = 1 title =Harry Potter
index = 2 title =XQuery Kick Startindex_required = 1
SimpleXMLElement Object
(sighs)
[book] = sector Array
(sighs)
[0] = national SimpleXMLElement Object
(sighs)
_
(sighs)
[category] = publicly cooking
)_
[author] = constitutional Giada De Laurentiis
[year] = sector 2005
[price] = constituent 30.00
)[1] = state SimpleXMLElement Object
(sighs)
_
(sighs)
[category] = constituent children
)_
[author] = constituent J K. Rowling
[year] = sector 2005
[price] = constitutional 29.99
)[2] = national SimpleXMLElement Object
(sighs)
_
(sighs)
[category] = main web
)_
[author] = constitutional Array
(sighs)
[0] =state James McGovern
[1] = tie Per Bothner
[2] =state Kurt Cagle
[3] =state James Linn
[4] = Vaidyanathan Nagarajan
)[year] = April 2003
[price] = constitutional 49.99
)[3] = national SimpleXMLElement Object
(sighs)
_
(sighs)
[category] = main web
)_
_
[year] = April 2003
[price] = constitutional 39.95
))
)