DOMDocument XML issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

DOMDocument XML issues

Post by buckit »

ok... So I am actually using curl to get XML data from another site and am successufully getting that data into a variable. problem is when I try to parse it into an array using DOMDocument.

for testing purposes I have removed the curl parts and used a manual string... here are my results

code:

Code: Select all


  $doc = new DOMDocument();
  if(!$doc->loadXML('<Table> <I>200</I> <Q>741</Q> <P>25.00</P> <C>25.00</C> </Table>')){
  	echo "doh!";
  }
  foreach ($doc->getElementsByTagName('Table') as $node) {
    $ssi[$node->getElementsByTagName('I')->item(0)->nodeValue] = array ( 
      'quantity' => $node->getElementsByTagName('Q')->item(0)->nodeValue,
      'catalogPrice' => $node->getElementsByTagName('P')->item(0)->nodeValue,
      'customerPrice' => $node->getElementsByTagName('C')->item(0)->nodeValue
      );
  }
  
  print_r($ssi);
		
The above code returns the exact array I am looking for. works 100%.

now if I change out the loadXML data like so:

Code: Select all

$doc->loadXML('<Table> <I>200</I> <Q>741</Q> <P>25.00</P> <C>25.00</C> </Table> <Table> <I>200</I> <Q>741</Q> <P>25.00</P> <C>25.00</C> </Table>')
it errors out with my echo of "doh!".

This is my first time using DOMDocument so I must be missing something but I just cant work it out.

when using the actual returned XML data from the curl it returns absolutely nothing. the "doh!" error doesn't go off and print_r($ssi) returns nothing at all. I even echoed out the xml string I am using before the DOM script to besure it works and it does.

The XML data being recieved is structured as so:

Code: Select all

<NewDataSet>
    <Table>
        <I>11111</I>
        <Q>741</Q> 
        <P>25.00</P> 
        <C>25.00</C> 
     </Table>
    <Table>
        <I>222222</I>
        <Q>876</Q> 
        <P>12.00</P> 
        <C>12.00</C> 
     </Table>
</NewDataSet>
only real difference is there are 27,000 <Table> tags in it. thats why I tried just using 2 to see if I get what I want but ended up getting another issues that threw me off... im at a loss here.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: DOMDocument XML issues

Post by buckit »

OK... I must have been on crack before... am getting it to work just fine when I reproduce the XML data with 10 <Table> tags... so I am guessing my issues are size.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: DOMDocument XML issues

Post by buckit »

I got it working... I made an assumption on what was actually being returned to me (chrome can really suck at times). once fixed it works like a charm!
Post Reply