xml - PHP DOMElement::getElementsByTagName - Anyway to get just the immediate matching children? -


Do you have a way to get immediate children, just by calling for the Claims: Millenniums Bitagnet? For example, I have an XML document in which the class element is I element in that category has sub-category elements (which have the same structure), such as:

  & lt; Category & gt; & Lt; ID & gt; 1 & lt; / Id & gt; & Lt; Name & gt; Top level category name & lt; / Name & gt; & Lt; Subcategory & gt; & Lt; ID & gt; 2 & lt; / Id & gt; & Lt; Name & gt; Sub-category name & lt; / Name & gt; & Lt; / Subcategory & gt; ... & lt; / Category & gt;  

If I represent a top level category, then

  $ topLevelCategoryElement-> GetElementsByTagName ('id');  

Will return a list with the nodes for all the 'id' elements, where I want only one top level any way to do this without using XPath?

I do not have to scare you have to walk through the kids again or use XPath. ($ N = $ parent-> First Child; $ n! == Zero; $ n = $ n- & gt; Next Sibling) {if ($ n Example Domalimation of & amp; amp; $ n- & gt; tagname == "xxx") {// ...}}

Example with XPath and your XML file:

  $ xml = ...; $ D = new DOMDocument (); $ D & gt; LoadXML ($ XML); $ Cat = $ d- & gt; GetElementsByTagName ("sub-category") - & gt; Item (0); $ Xp = new DOMXpath ($ d); $ Q = $ xp- & gt; Query ("id", $ cat); // echo the second argument $ q- & gt; Items (0) - & gt; TextContent;  

returns 2 .


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -