0

I'm getting some data through an API and it's returning XML in the shape of:

<SUBSCRIBER_ROW>
<SUBSCRIBER>
    <ACCOUNT_NUMBER Value="0123456789123"/>
    <FIRST_NAME Value="fakeFirstName         "/>
    <INITIAL Value="w "/>
    <SURNAME Value="fakeLastName        "/>
    <TELEPHONE_NUMBER Value="5551234"/>
    <TELEPHONE_AREA Value="403"/>

The code I have to pull out the data is as follows:

   protected function par_getSubscriber($webCBSXML, $isJSON = false){
  ////parse XML
    $msg = 'WebCBS ERR: ';
    $dom = new DOMDocument();
    if(@$dom->loadXML($webCBSXML)) {
        $errors = $dom->getElementsByTagName('Error');
        if($parsedData = self::checkError($dom)) {
            $parsedData = self::formatOutput($parsedData, $isJSON, true);
            return $parsedData;
        } else {
            $subscriber['account_number']      = $dom->getElementsByTagName('account_number')->getAttribute('Value');
            $subscriber['first_name']          = $dom->getElementsByTagName('first_name')->getAttribute('Value');
            $subscriber['surname']             = $dom->getElementsByTagName('surname')->getAttribute('Value');
            $subscriber['telephone_number']    = $dom->getElementsByTagName('telephone_number')->getAttribute('Value');

However, the whole script dies right after this line:

$subscriber['account_number']      = $dom->getElementsByTagName('account_number')->getAttribute('Value');

I don't have much experience dealing with DOM documents and since it's creating an object out of the XML I'm unable to use var_dump/print_r to see the structure of it!

1
  • 2
    Don't forget that XML element names are case-sensitive. Commented Jan 7, 2013 at 23:51

1 Answer 1

1

Although I would suggest to use XPath to extract the information you need, it seems that you just misunderstood the return value of getElementsByTagName(). It returns a DOMNodeList not a single node. Also you have to uppercase the node names as @salathe mentioned. Change the code to something like :

$subscriber['account_number'] =
    $dom->getElementsByTagName('ACCOUNT_NUMBER')->item(0)->getAttribute('Value');

Also you should check that the nodes a really present in the XML. You can write the checks in PHP or you can use a schema language like XSD.

Sign up to request clarification or add additional context in comments.

5 Comments

Even when I use:` $subscriber['account_number'] = $dom->getElementsByTagName('account_number')->item(0)->getAttribute('Value');` the code is dying right after that line. If I echo before and after that line the line before echo's, the line after does not.
What does the error log say? Can you ini_set('display_errors', 1); just before the line?
I love you for that line. I couldn't get errors to display before! Fatal error: Call to a member function getAttribute() on a non-object in /home/bkilshaw/public_html/webcbs/Class-WebCBS.php on line 1196
Hehe.. Don't forget to disable it for production. Instead of using ini_get() in the code directly you should consider to set it in the php.ini. Disabled on production servers, may be enabled on development servers. If using display_errors wont fit because it breaks something (maybe JSON responses etc) then don't forget that you have a log file! ;)
The comment on my original question, mixed with your reply, got me the answer. The XML is case sensitive so `$subscriber['account_number'] = $dom->getElementsByTagName('ACCOUNT_NUMBER')->item(0)->getAttribute('Value'); worked great!

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.