How to get innerHTML of XML tag has specific id attribute using php? -
having xml file, how can content with/without attribute? example having as:
<book id="1"> <title>...</title> <author>...</author> </book> <book id="2"> <title>...</title> <author>...</author> </book> <book id="3"> <title>...</title> <author>...</author> </book>
giving input tagname book
, id
2
, should return inside it, in case:
<title>...</title> <author>...</author>
i have solved problem looking xml file string , working string want ask possible using xml-file without scroll directly content. in real have big xml file (something 30-40 mb) , want search content in giving coordinates tag-name , (if present) attribute/s. structure of file not "constant".
domxpath::query
method filter selected elements xpath
. can select elements has specific attribute. code find book
tag value of id
attribute of 2
.
$dom = new domdocument; $dom->loadxml($str); $xpath = new domxpath($dom); $book = $xpath->query("//book[@id='2']")->item(0); foreach($book->childnodes $node) { @$html .= $dom->savehtml($node); } echo $html;
note final part of code loop through childs of selected element html of them.
check result in demo
Comments
Post a Comment