Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>DOMDocument stuff can be tricky to get a handle on, because functions such as <code>print_r()</code> and <code>var_dump()</code> don't necessarily perform the same as they would on normal arrays and objects (see <a href="http://www.php.net/manual/en/class.domelement.php#86224" rel="nofollow noreferrer">this comment</a> in the manual).</p> <p>You have to use various functions and properties of the document nodes to pull out the data. For instance, if you had the following XML:</p> <pre><code>&lt;LineItemInfo attr1="hi"&gt;This is a line item.&lt;/LineItemInfo&gt; </code></pre> <p>You could output various parts of that using:</p> <pre><code>$itemListObjects = $orderXML-&gt;getElementsByTagName('LineItemInfo'); foreach($itemListObjects as $node) { echo $node-&gt;nodeValue; //echos "This is a line item." echo $node-&gt;attributes-&gt;getNamedItem('attr1')-&gt;nodeValue; //echos "hi" } </code></pre> <p>If you had a nested structure, you can follow basically the same procedure using the <code>childNodes</code> property. For example, if you had this:</p> <pre><code>&lt;LineItemInfo attr1="hi"&gt; &lt;LineItem&gt;Line 1&lt;/LineItem&gt; &lt;LineItem&gt;Line 2&lt;/LineItem&gt; &lt;/LineItemInfo&gt; </code></pre> <p>You might do something like this:</p> <pre><code>$itemListObjects = $orderXML-&gt;getElementsByTagName('LineItemInfo'); foreach($itemListObjects as $node) { if ($node-&gt;hasChildNodes()) { foreach($node-&gt;childNodes as $c) { echo $c-&gt;nodeValue .","; } } } //you'll get output of "Line 1,Line 2," </code></pre> <p>Hope that helps.</p> <p><strong>EDIT for specific code and XML</strong></p> <p>I ran the following code in a test script, and it seemed to work for me. Can you be more specific about what's not working? I used your code exactly, except for the first two lines that create the document. Are you using <code>loadXML()</code> over <code>loadHTML()</code>? Are there any errors?</p> <pre><code>$orderXML = new DOMDocument(); $orderXML-&gt;loadXML(" &lt;LineItems&gt; &lt;LineItemInfo&gt; &lt;ProductId href='[URL_TO_PRODUCT_XML]'&gt;149593&lt;/ProductId&gt; &lt;/LineItemInfo&gt; &lt;LineItemInfo&gt; &lt;ProductId href='[URL_TO_PRODUCT_XML]'&gt;149593&lt;/ProductId&gt; &lt;/LineItemInfo&gt; &lt;/LineItems&gt; "); $itemListObject = $orderXML-&gt;getElementsByTagName('LineItemInfo'); foreach ($itemListObject as $element) { $product = $element-&gt;getElementsByTagName('ProductId'); $productId = $product-&gt;item(0)-&gt;nodeValue; echo $productId.'-'; } //outputs "149593-149595-" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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