1

I have the following XML output from an API

<?xml version='1.0' encoding='UTF-8'?>
<Response>
    <ReturnRow Output1="11" Output2="12" Output3="13" Output4="14" Output5="15" />
    <ReturnRow Output1="21" Output2="22" Output3="23" Output4="24" Output5="25" />
    <Messages>
        <Message Code="INFO" Msg="Your request is successful." Type="APP"/>
    </Messages>       
</Response>

I'm trying to parse the above xml using PHP, loop the child element ReturnRown and echo out the Output1, Output2 into an HTML table.

Here's my code currently:

$doc = new DOMDocument();
$doc->loadXML($test); //$test is holding the above XML
$node = $doc->getElementsByTagName( "ReturnRow" );
foreach ($node as $book) {
     var_dump($book);
}

But the above is not actually giving me the values of the Ouput1, Output2 etc.

What am I missing or doing wrong?

The result of my above PHP code:

object(DOMElement)#3 (18) { ["tagName"]=> string(9) "ReturnRow" ["schemaTypeInfo"]=> NULL        ["nodeName"]=> string(9) "ReturnRow" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1)     ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(9) "ReturnRow" ["baseURI"]=> string(20) "file:///W:/ischools/" ["textContent"]=> string(0) "" } object(DOMElement)#5 (18) { ["tagName"]=> string(9) "ReturnRow" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(9) "ReturnRow" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(9) "ReturnRow" ["baseURI"]=> string(20) "file:///W:/ischools/" ["textContent"]=> string(0) "" }
3
  • What does it give you? Commented Aug 8, 2012 at 15:39
  • possible duplicate of How to parse and process HTML with PHP? Commented Aug 8, 2012 at 15:39
  • Please edit you OP with the info :) Commented Aug 8, 2012 at 15:45

1 Answer 1

2

In your case, i would prefer to use simplexml because it could return all attributes of a node in an easily iterable format, and use xpath to find the nodes you are interested in:

$xml = '<'.'?xml version="1.0" encoding="UTF-8"?>
<Response>
    <ReturnRow Output1="11" Output2="12" Output3="13" Output4="14" Output5="15" />
    <ReturnRow Output1="21" Output2="22" Output3="23" Output4="24" Output5="25" />
    <Messages>
        <Message Code="INFO" Msg="Your request is successful." Type="APP"/>
    </Messages>
</Response>
';

$doc = simplexml_load_string($xml);
$node = $doc->xpath('//ReturnRow');
foreach ($node as $book) {
    foreach ($book->attributes() as $name => $value) {
        var_dump((string)$name, (string)$value);
    }
}

If your input has always have these attributes you can use DOMNode's getAttribute() method in your original code's loop to get to the values:

foreach ($node as $book) {
    var_dump($book->getAttribute('Output1'), $book->getAttribute('Output2'));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. First code blocks works and dump out the values. But second code block causes a php error. any ideas?
I'm not sure why would it throw errors, here's a full demo that should work without erros: codepad.viper-7.com/tYNMzI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.