无法从simplexml对象提取数据

时间:2018-10-19 16:43:54

标签: php simplexml

我从以下位置获取了一个simplexml对象:

$xml = simplexml_load_string($data);

如果我做dd($xml),则会在控制台中得到以下字符串:

SimpleXMLElement {#232
  +"Data": SimpleXMLElement {#240}
  +"Error": SimpleXMLElement {#241}
  +"xmlData": "<root><row><FiscalCode>27221245</FiscalCode><CompanyName>Nume Companie SRL</CompanyName><LegalForm>SRL</LegalForm><RegistrationNumber>J40/111/2010</RegistrationNumber><DateOfIncorporation>2010-07-29</DateOfIncorporation><Status>functiune;</Status><ShareCapital>1.510000000000000e+005</ShareCapital><NACE>6201</NACE><NACE_Descr>Activităţi de realizare a soft-ului la comandă (software orientat client)</NACE_Descr><PrimaryActivityVersion>2</PrimaryActivityVersion><NumberOfemployees>20</NumberOfemployees><County>Municipiul Bucuresti</County><City>BUCURESTI SECTORUL 5</City><Street>DR. JOSEPH LISTER</Street><Number>22</Number><Block /><Entrance /><Floor /><Apartment>1</Apartment><PostalCode>50543</PostalCode><PhoneNumber>0724063943</PhoneNumber><E-mail /><Web /><OwnershipForm /><CompanyIdentification>certificat de înmatriculare preschimbat; </CompanyIdentification><UpdateOn>2018-07-04</UpdateOn><Headquarters_DocumentType>Contract de închiriere</Headquarters_DocumentType><Headquarters_DocumentDuringType>Temporar</Headquarters_DocumentDuringType><Headquarters_DocumentNumber>1</Headquarters_DocumentNumber><Headquarters_DocumentDate>01-11-2016</Headquarters_DocumentDate><HeadquartersExpiryDayFrom>2016-11-01</HeadquartersExpiryDayFrom><HeadquartersExpiryDayUntil>2019-11-01</HeadquartersExpiryDayUntil><DateofIncorporationDocument>2018-07-03</DateofIncorporationDocument><SecondaryHeadquarters><row><Name>Punct de lucru</Name><StartDate /><StopDate /><Country>ROMANIA</Country><Locality>BUCUREŞTI SECTORUL 5</Locality><Street>DR.  STAICOVICI</Street><Number>19</Number><Block /><Entrance /><Floor>1</Floor><Apartment>2</Apartment><District>5</District><PostalCode /></row><row><Name>PUNCT DE LUCRU</Name><StartDate>2013-05-01 00:00:00.0000000</StartDate><StopDate>2014-05-01 00:00:00.0000000</StopDate><Country>ROMANIA</Country><Locality>BUCUREŞTI SECTORUL 5</Locality><Street>B-dul EROILOR</Street><Number>13</Number><Block /><Entrance /><Floor /><Apartment>2</Apartment><District>5</District><PostalCode /></row></SecondaryHeadquarters></row></root>"
}

提取 FiscalCode

的正确方法

我提到 dd($xml->FiscalCode)不返回任何内容。

dd($xml->root->row->FiscalCode);不起作用

原始xml是从外部链接获取的:

看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    <Data>
        <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
            <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                <xs:complexType>
                    <xs:choice minOccurs="0" maxOccurs="unbounded" />
                </xs:complexType>
            </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" />
    </Data>
    <Error />
    <xmlData>&lt;root&gt;&lt;row&gt;&lt;FiscalCode&gt;27225145&lt;/FiscalCode&gt;&lt;CompanyName&gt;Nume Companie SRL&lt;/CompanyName&gt;&lt;LegalForm&gt;SRL&lt;/LegalForm&gt;&lt;RegistrationNumber&gt;J40/7213/2010&lt;/RegistrationNumber&gt;&lt;DateOfIncorporation&gt;2010-07-29&lt;/DateOfIncorporation&gt;&lt;Status&gt;functiune;&lt;/Status&gt;&lt;ShareCapital&gt;1.510000000000000e+005&lt;/ShareCapital&gt;&lt;NACE&gt;6201&lt;/NACE&gt;&lt;NACE_Descr&gt;Activităţi de realizare a soft-ului la comandă (software orientat client)&lt;/NACE_Descr&gt;&lt;PrimaryActivityVersion&gt;2&lt;/PrimaryActivityVersion&gt;&lt;NumberOfemployees&gt;20&lt;/NumberOfemployees&gt;&lt;County&gt;Municipiul Bucuresti&lt;/County&gt;&lt;City&gt;BUCURESTI SECTORUL 5&lt;/City&gt;&lt;Street&gt;DR. JOSEPH LISTER&lt;/Street&gt;&lt;Number&gt;22&lt;/Number&gt;&lt;Block /&gt;&lt;Entrance /&gt;&lt;Floor /&gt;&lt;Apartment&gt;1&lt;/Apartment&gt;&lt;PostalCode&gt;50543&lt;/PostalCode&gt;&lt;PhoneNumber&gt;0724063943&lt;/PhoneNumber&gt;&lt;E-mail /&gt;&lt;Web /&gt;&lt;OwnershipForm /&gt;&lt;CompanyIdentification&gt;certificat de înmatriculare preschimbat; &lt;/CompanyIdentification&gt;&lt;UpdateOn&gt;2018-07-04&lt;/UpdateOn&gt;&lt;Headquarters_DocumentType&gt;Contract de închiriere&lt;/Headquarters_DocumentType&gt;&lt;Headquarters_DocumentDuringType&gt;Temporar&lt;/Headquarters_DocumentDuringType&gt;&lt;Headquarters_DocumentNumber&gt;1&lt;/Headquarters_DocumentNumber&gt;&lt;Headquarters_DocumentDate&gt;01-11-2016&lt;/Headquarters_DocumentDate&gt;&lt;HeadquartersExpiryDayFrom&gt;2016-11-01&lt;/HeadquartersExpiryDayFrom&gt;&lt;HeadquartersExpiryDayUntil&gt;2019-11-01&lt;/HeadquartersExpiryDayUntil&gt;&lt;DateofIncorporationDocument&gt;2018-07-03&lt;/DateofIncorporationDocument&gt;&lt;SecondaryHeadquarters&gt;&lt;row&gt;&lt;Name&gt;Punct de lucru&lt;/Name&gt;&lt;StartDate /&gt;&lt;StopDate /&gt;&lt;Country&gt;ROMANIA&lt;/Country&gt;&lt;Locality&gt;BUCUREŞTI SECTORUL 5&lt;/Locality&gt;&lt;Street&gt;DR.  STAICOVICI&lt;/Street&gt;&lt;Number&gt;19&lt;/Number&gt;&lt;Block /&gt;&lt;Entrance /&gt;&lt;Floor&gt;1&lt;/Floor&gt;&lt;Apartment&gt;2&lt;/Apartment&gt;&lt;District&gt;5&lt;/District&gt;&lt;PostalCode /&gt;&lt;/row&gt;&lt;row&gt;&lt;Name&gt;PUNCT DE LUCRU&lt;/Name&gt;&lt;StartDate&gt;2013-05-01 00:00:00.0000000&lt;/StartDate&gt;&lt;StopDate&gt;2014-05-01 00:00:00.0000000&lt;/StopDate&gt;&lt;Country&gt;ROMANIA&lt;/Country&gt;&lt;Locality&gt;BUCUREŞTI SECTORUL 5&lt;/Locality&gt;&lt;Street&gt;B-dul EROILOR&lt;/Street&gt;&lt;Number&gt;13&lt;/Number&gt;&lt;Block /&gt;&lt;Entrance /&gt;&lt;Floor /&gt;&lt;Apartment&gt;2&lt;/Apartment&gt;&lt;District&gt;5&lt;/District&gt;&lt;PostalCode /&gt;&lt;/row&gt;&lt;/SecondaryHeadquarters&gt;&lt;/row&gt;&lt;/root&gt;</xmlData>
</Result>

我得到这样的链接:

       $curl = curl_init();

            curl_setopt_array($curl, Array(
                CURLOPT_URL            => 'https://wsprokolekt.rocomp.ro/service.asmx/CompanyGeneralInformation?idUser=*****&CUI=******',
                CURLOPT_RETURNTRANSFER => TRUE,
                CURLOPT_ENCODING       => 'UTF-8'
            ));

            $data = curl_exec($curl);
$xml = simplexml_load_string($data);

谢谢您的时间!

1 个答案:

答案 0 :(得分:1)

首先,读取和解码xmlData,然后获取FiscalCode

$xml = simplexml_load_string($str);

$xml1 = simplexml_load_string(html_entity_decode($xml->xmlData));
echo $xml1->row->FiscalCode;