0

Hello I would like to get access to some data in a xml using xpath but I don't know how to achieve it, The aim is to get the result like this:

1245, 00185, 10, new york
1245, 00185, 5, london
1246, 00186, 10, madrid
1246, 00186, 5, paris
1246, 00186, 8, munich

The xml is this one:

<?xml version="1.0"?>
<result>
<registry code="1245">
<PR_COD>00185</PR_COD>
<PR_DRE ncols="2">
  <row>
    <column_1>10</column>
    <column_2>new york</column>
  </row>
  <row>
    <column_1>5</column>
    <column_2>london</column>
  </row>
</PR_DRE>
</registry>
<registry code="1246">
<PR_COD>00186</PR_COD>
<PR_DRE ncols="3">
  <row>
    <column_1>10</column>
    <column_2>madrid</column>
  </row>
  <row>
    <column_1>5</column>
    <column_2>paris</column>
  </row>
  <row>
    <column_1>8</column>
    <column_2>munich</column>
  </row>
</PR_DRE>
</registry>
</result>
Fran Rod
  • 586
  • 4
  • 14
  • 26
  • What have you tried? Do you know [DOMXpath:evaluate()](http://php.net/domxpath)? – ThW Jul 22 '16 at 07:25

1 Answers1

0

You can use SimpleXML to parse XML document:

$xml = new SimpleXMLElement($string);
$result = $xml->xpath('.//PR_DRE/row/*');

or

$xml = new SimpleXMLElement('file.xml', null, true);
$result = $xml->xpath('.//*/*/*');
Vasin Yuriy
  • 484
  • 5
  • 13
  • Hi I have get the wished result using a foreach inside the main foreach calling to the parent node. Thank you foreach ( $cities->xpath('/*/*') as $cod) { $pr_cod = $cod->PR_COD; } – Fran Rod Jul 23 '16 at 09:48
  • But I have a problem now, I m always receiving the last value of the all parent nodes instead the value of each element – Fran Rod Jul 23 '16 at 11:45
  • @FranRod, еach element has one parent, so you can get parrents and then get all childs for them. – Vasin Yuriy Jul 24 '16 at 12:25
  • would you mind to show me how to do it ? I m trying and I only get the the first children value of each parent – Fran Rod Jul 24 '16 at 18:19
  • 1
    hi, I have already achieved what I want, i got the reference from here http://stackoverflow.com/questions/11238878/xml-with-varying-amount-of-child-nodes-for-each-parent-node Thanks for everyone who helped me. – Fran Rod Jul 24 '16 at 23:34
  • @FranRod, good job. – Vasin Yuriy Jul 25 '16 at 18:00