Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

php SimpleXML attributes are missing

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...