Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP, XML Get a records nodes and values and put them in a JSON Array?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3448005/implementing-condition-in-xpath-and-xquery">Implementing condition in XPath and XQuery</a><br> <a href="https://stackoverflow.com/questions/8830599/php-convert-xml-to-json">PHP convert XML to JSON</a> </p> </blockquote> <p>How do i get all child notes from the record which includes Product = Product1 And put them into a JSON array ?</p> <pre><code>&lt;RECORD&gt; &lt;PROP NAME="Product"&gt; &lt;PVAL&gt;&lt;!CDATA[Produkt1]]&lt;/PVAL&gt; &lt;PROP NAME="Value"&gt; &lt;PVAL&gt;&lt;!CDATA[10]]&lt;/PVAL&gt; &lt;PROP NAME="Status"&gt; &lt;PVAL&gt;&lt;!CDATA[Active]]&lt;/PVAL&gt; &lt;/RECORD&gt; &lt;RECORD&gt; &lt;PROP NAME="Product"&gt; &lt;PVAL&gt;&lt;!CDATA[Produkt2]]&lt;/PVAL&gt; &lt;PROP NAME="Value"&gt; &lt;PVAL&gt;&lt;!CDATA[20]]&lt;/PVAL&gt; &lt;PROP NAME="Status"&gt; &lt;PVAL&gt;&lt;!CDATA[Active]]&lt;/PVAL&gt; &lt;/RECORD&gt; &lt;RECORD&gt; &lt;PROP NAME="Product"&gt; &lt;PVAL&gt;&lt;!CDATA[Produkt3]]&lt;/PVAL&gt; &lt;PROP NAME="Value"&gt; &lt;PVAL&gt;&lt;!CDATA[30]]&lt;/PVAL&gt; &lt;PROP NAME="Status"&gt; &lt;PVAL&gt;&lt;!CDATA[Active]]&lt;/PVAL&gt; &lt;/RECORD&gt; </code></pre> <p>I need the data for an iPad App, calling something like www.products.com/product.php?Product1</p> <p>Having the JSON look something like</p> <pre><code>{ "Product": [ { "Name":"Product1" , "Value":"10" , "Status":"Active" } ] } </code></pre> <p>Edit : Solution with XML reader</p> <pre><code>&lt;?php $z = new XMLReader; $z-&gt;open('products.xml'); $doc = new DOMDocument; // move to the first &lt;product /&gt; node while ($z-&gt;read() &amp;&amp; $z-&gt;name !== 'RECORD'); // now that we're at the right depth, hop to the next &lt;product/&gt; until the end of the tree while ($z-&gt;name === 'RECORD') { $node = simplexml_import_dom($doc-&gt;importNode($z-&gt;expand(), true)); $strvalue = $node-&gt;PROP[6]-&gt;PVAL; echo $strvalue."&lt;p&gt;" ; // go to next &lt;product /&gt; $z-&gt;next('RECORD'); } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload