xml - Push nested tag value into PHP array -


How can you press sample_id value by using the PHP DOM and together before and samples ?

I have tried to add another forward loop, but I am getting various notices like

  trying to get non-object property   

XML

  & lt ;? Xml version = "1.0" encoding = "ISO-8,859-1" & gt; & Lt; Maintable & gt; & Lt; Groups & gt; & Lt; First & gt; Text 1 & lt; / First & gt; & Lt; Second & gt; Text 2 & lt; / Second & gt; & Lt; Samples a = "link" b = "link" & gt; & Lt; Sample_id & gt; 000,555 & lt; / Sample_id & gt; & Lt; / Samples & gt; & Lt; / Groups & gt; & Lt; Groups & gt; & Lt; First & gt; Text 3 & lt; / First & gt; & Lt; Second & gt; Text 4 & lt; / Second & gt; & Lt; Samples a = "link" b = "link" & gt; & Lt; Sample_id & gt; 111,000 & lt; / Sample_id & gt; & Lt; / Samples & gt; & Lt; / Groups & gt; & Lt; / Maintaining & gt;   

PHP

  & lt; Php $ text = file_get_contents ($ file); $ Content = iconv ('UTF-8', 'UTF-8 // IGNORE', $ text); $ Xml = New DOMDocument ('1.0', 'ISO-885 9-1'); $ Xml- & gt; LoadXML ($ content); $ Xpath = new DOMXPath ($ xml); $ Query = '/ Maintenance / Group [first = "lesson 3"]'; $ Node = $ xpath- & gt; Query ($ query); $ Var = array (); Foreach ($ n = $ K = $ node; $ node) {foreach ($ node-> $ cnode as childNodes) {if ($ cnode-> nodename == 'first') {$ [$ K] above ["first"] = (string) $ cnode-> TextContent; } If ($ cnode-> nodename == 'samples') {$ var [$ k] ["samp_url"] = (string) $ cnode- & gt; GetAttribute ("a"); } If ($ cnode-> nextSibling-> nodename == 'sample_id') {$ over [$ K] ["sample_id"] = (string) $ cnode-> NextSibling-> TextContent; }}} // print_r ($ cnode-> child nodes); ? & Gt;   

Try to loop through the node list

  $ var = array (); $ Node = $ xpath- & gt; Rate ('/ maintenance / group [first = "lesson 3"]'); Foreach ($ nodes as $ nodes) {foreach ($ node-> $ cnode as childNodes) {if (is_array ($ cnode)) {foreach ($ cnode-> ChildNodes $ as CN ) {$ Over [] ["Sample_id"] = (string) $ cn- & gt; TextContent; }}}} Var_dump ($ var);    

DOMXpath :: query () can return only nodes You can also return lists, DOMXpath :: evaluation () scalar, too. If the second logic of the query () / evaluation () is provided in the reference node, then the Xpath expression will be relative to it.

Then you can select the nodes first, iterate the list and use evaluation () to get the value: < Pre> $ Xml = new DOMDocument (); $ Xml- & gt; LoadXML ($ content); $ Xpath = new DOMXPath ($ xml); $ Result = []; $ Node = $ xpath- & gt; Rate ('/ maintenance / group [first = "lesson 3"]'); Foreign Currency ($ node $ node form) {$ result [] = ['first' = & gt; $ Xpath- & gt; Evaluate ('string (first)', $ node), 'sample_url' = & gt; $ Xpath- & gt; Evaluate ('string (samples / @a)', $ node), 'sample_id' = & gt; $ Xpath- & gt; Rate ('String (Sample / Sample_ID)', $ node)]; } Var_dump ($ result);

Output:

  array (1) {[0] = & gt; Array (3) {["first"] = & gt; String (5) "text3" ["sample_url ="] = & gt; String (4) "link" ["sample_id"] = & gt; The string (6) "111000"}}   

brings only sample IDs from the document work, too:

  var_dump ($ xpath-> evaluation ('String (/ maintenance / group [first = "text3"] / sample / sample_id'));   

Output:

  string (6 ) "111000"    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -