php - Using simplexml to get a node (and its children) by attribute -


Using Simplex, is it possible to get children of nodes on the basis of parental characteristics? For example, I want to get a list of variants in myItem1 (output will be small, bigger)

Here are some psuedo codes that I am following:

  Foreign currency ($ xml-> xpath ('// family [@ name = "myItem1"]') -> Variant-> children () $ $ child) {$ child-> ; GetName ()}  

(Of course this is not working, but hopefully it tells me what I am trying to do.)

Sample XML:

  & lt; Library & gt; & Lt; Family name = "myItem1" & gt; & Lt; Variants & gt; & Lt; Small & gt; ... & lt; / Small & gt; & Lt; Large & gt; ... & lt; / Large & gt; & Lt; / Variants & gt; & Lt; / Family & gt; & Lt; Family name = "myItem2" & gt; & Lt; Variants & gt; & Lt; Small & gt; ... & lt; / Small & gt; & Lt; Medium & gt; ... & lt; / Medium & gt; & Lt; / Variants & gt; & Lt; / Family & gt; & Lt; / Library & gt;  

Thanks.

If I understand correctly, you want to get names of all the children in the given node There are several ways to do this, one of them is to walk all the children again, like this:

  foreach ($ xml-> xpath ('// family [@ name = "MyItem1"] ') as $ family {$ variables = array (); Foreign Currency ($ family-> version-> children () $ name = & gt; as $ node) {$ version [] = $ name; } // If you are a duplicate, you can $ variants = array_unique ($ version); Var_dump ($ variants); }  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -