6

I have the following xml document here: Edit: (see below for sample)

I am using php/SimpleXML to covert it to an object to read it:

$xmlContent = file_get_contents($path . '/test.xml');
$tablesRaw = new SimpleXMLElement($xmlContent);
echo '<pre>';
print_r($tablesRaw);
echo '</pre>';

When I print_r I see attributes for field but attributes for acceptable-value do not show. Here is an example of the raw xml (I need the value attribute):

<acceptable-value value="0">
    Unknown
</acceptable-value>

This is what I see when I print_r:

[acceptable-values] => SimpleXMLElement Object
                                            (
                                                [acceptable-value] => Array
                                                    (
                                                        [0] => 
                    Unknown

                                                        [1] => 
                    Invalid

                                                        [2] => 
                    Deleted

                                                        [3] => 
                    Valid/Good

                                                        [4] => 
                    Inactive

                                                    )

                                            )

Any clues why the attributes are not showing? Thanks in advance.

EDIT: Request for some of the xml:

<field name="Address1Type" type="String"/>
<field name="Address2Street1" type="String"/>
<field name="Address2Street2" type="String"/>
<field name="Address2Type" type="String"/>
<field name="Address3Street1" type="String"/> 
<field name="Status" type="Integer" access="R">
            <acceptable-values>
                <acceptable-value value="0">
                    Unknown
                </acceptable-value>
                <acceptable-value value="1">
                    Invalid
                </acceptable-value>
                <acceptable-value value="2">
                    Deleted
                </acceptable-value>
                <acceptable-value value="3">
                    Valid/Good
                </acceptable-value>
                <acceptable-value value="4">
                    Inactive
                </acceptable-value>
            </acceptable-values>
        </field>
4
  • Will you copy/paste some of the original xml code, so I don't have to download it? Commented Sep 14, 2012 at 22:26
  • yeah, trying to get some to show Commented Sep 14, 2012 at 22:29
  • What version of PHP? It prints the attributes for me.. Commented Sep 14, 2012 at 22:46
  • Version 5.2.13. It prints the attribute 'value' specifically on acceptable-value? Commented Sep 14, 2012 at 22:57

2 Answers 2

10

The simple answer here is not to use print_r() with SimpleXML objects. Because they are wrappers around non-PHP data, functions like that which would normally show the "whole" object don't really reflect what you're looking at.

The way to access an attribute with SimpleXML is to use the attribute name as though it was an array key ($node['attribute']); this does not mean that there is an array somewhere with that key, it is a function-call in disguise.

If you want to get a feel for which nodes you're looking at while writing SimpleXML code, check out this simplexml_dump() function which I wrote (feedback welcome).

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

5 Comments

Not sure if I am doing something wrong but it looks like it is not working for me, you can see my code/output here i46.tinypic.com/wnqti.jpg
@joshmmo Apologies, I tend to run PHP with Notices suppressed, as they often complain about things I am happy with. I will update the function not to raise that Notice when I next log onto to my dev box. The output however is as expected - it doesn't attempt to recurse through the whole structure, just tell you that the current node is called 'tables' and contains 57 children called 'table'. simplexml_dump($tablesRaw->children()) shows something more useful, or indeed simplexml_dump($tablesRaw->table[22]->fields->field[31]->{'acceptable-values'}->children())
very nice I like it. It would be nice as well if it could expand the whole object so to speak so I can see it all at once.
@joshmmo I've been considering what the best way to represent the "full" document/sub-tree would be, but struggling to think of one that would be any more readable than ->asXML()...
Your simplexml_tree function was very useful for debugging, thanks!
1

First, you have to get the SimpleXMLElement object. In this case:

$xmlContent = file_get_contents($path . '/test.xml');
$tablesRaw = new SimpleXMLElement($xmlContent);
$elements = $tablesRaw->table[22]->fields->field[31]->{'acceptable-values'}->children();

Now, you can iterate over each acceptable-value object and use the attributes() method:

foreach($elements as $element) {
    echo $element->attributes()->value . " ";
    echo trim($element[0]) . "\n";
}

With your XML, that will print:

0 Unknown
1 Invalid
2 Deleted
3 Valid/Good
4 Inactive

It doesn't rely on the array index, because the call to attributes() gets the actual attributes of the element. And ->value gets the attribute with the name "value".

Comments

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.