使用SimpleXMLElement提取元素

时间:2011-12-20 21:58:37

标签: php simplexml

我一直试图让SimpleXMLElement从每个php.net示例中使用以下代码从XML文件中提取vsingle值:

$resp = new SimpleXMLElement($xml);
foreach($resp->SESSION_LIST->SESSION as $sess){
    $type = $sess->TRACKING->TYPE;
}

当我这样做时,返回值总是另一个simpleXML对象,我不知道如何获得我正在寻找的值。有人可以帮我推动正确的方向吗?

这是我正在使用的XML字符串。它可以有多个SESSION属性:

$xml = <<<XML
<?xml version="1.0"?>
    <FREIGHTVIEW>
        <SESSION_LIST>
            <SESSION id="529075">
                <SESSION_ID>529075</SESSION_ID>
                <LABEL>116800</LABEL>
                <INTEGRATION_REFERENCE_NUMBER>WDLSTRFDTS</INTEGRATION_REFERENCE_NUMBER>
                <TRACKING>
                <TYPE>provider assigned</TYPE> 
                <EVENT_DATETIME>03/01/11 02:07:59 PM</EVENT_DATETIME> 
                <TIMEZONE /> 
                <USER_NAME>Dispatch Team</USER_NAME> 
                <COMPANY_NAME>M&amp;W Distribution</COMPANY_NAME> 
                <RECORDED_DATETIME>03/01/11 02:07:59 PM</RECORDED_DATETIME> 
                <SOURCE /> 
            </TRACKING> 
            <QUOTE id="5173468"> 
                <QUOTE_ID>5173468</QUOTE_ID> 
                <CARRIER type="Private"> 
                    <NAME>- FedEx Freight Priority -</NAME> 
                    <CARRIER_REFERENCE_NUMBER>FXFE</CARRIER_REFERENCE_NUMBER> 
                    <CONTACT>- FedEx Freight Priority -</CONTACT> 
                    <EMAIL /> 
                    <PHONE></PHONE> 
                </CARRIER> 
                <TOTAL_AMOUNT>262.57</TOTAL_AMOUNT> 
                <ACCESSORIALS> 
                    <ACCESSORIAL> 
                        <NAME>fuel surcharge</NAME> 
                        <AMOUNT>79.47</AMOUNT> 
                    </ACCESSORIAL>
                    <ACCESSORIAL> 
                        <NAME>delivery appointment request</NAME> 
                        <AMOUNT>0.00</AMOUNT> 
                    </ACCESSORIAL> 
                    <ACCESSORIAL> 
                        <NAME>line haul</NAME> 
                        <AMOUNT>183.10</AMOUNT> 
                    </ACCESSORIAL> 
                </ACCESSORIALS> 
                <EQUIPMENT> 
                    <MODE>ltl</MODE> 
                    <TYPE></TYPE> 
                </EQUIPMENT> 
                <PICKUP_DATE></PICKUP_DATE> 
                <ESTIMATED_DELIVERY_DATE></ESTIMATED_DELIVERY_DATE> 
                <REFERENCE_ID>37307086</REFERENCE_ID> 
                <QUOTE_GOOD_UNTIL></QUOTE_GOOD_UNTIL> 
                <QUOTE_TYPE>instant ltl</QUOTE_TYPE> 
                <COMMENTS></COMMENTS> 
                <QUOTE_STATUS>assigned</QUOTE_STATUS> 
            </QUOTE> 
        </SESSION> 
    </SESSION_LIST> 
</FREIGHTVIEW>
XML;

1 个答案:

答案 0 :(得分:2)

您需要将对象强制转换为字符串:

$type = (string) $sess->TRACKING->TYPE;