如何将该字符串转换为可以访问节点的XML文档?

时间:2019-05-09 14:51:26

标签: php xml simplexml-load-string

我正在使用此SOAP Web服务,当我调用getData方法时会给出以下响应:

{#266 ▼
  +"GetDataResult": {#267 ▼
    +"string": """
      <?xml version="1.0"?>
      <maxs:Customers xmlns:maxs="http://www.microarea.it/Schema/2004/Smart/ERP/CustomersSuppliers/Customers/Users/sa/InfinitySyncroConnector.xsd" tbNamespace="Document.ERP.CustomersSuppliers.Documents.Customers" xTechProfile="InfinitySyncroConnector"><maxs:Data><maxs:CustomersSuppliers master="true"><maxs:CustSuppType>3211264</maxs:CustSuppType><maxs:CustSupp>021061</maxs:CustSupp><maxs:CompanyName>Luigi Dal XXXXo &amp; C. S.A.S. (XXXX)</maxs:CompanyName><maxs:ISOCountryCode>IT</maxs:ISOCountryCode><maxs:TaxIdNumber>XXXXXXXX</maxs:TaxIdNumber><maxs:FiscalCode>XXXXXXXXX</maxs:FiscalCode><maxs:CustSuppKind>XXXXXXX</maxs:CustSuppKind><maxs:Address>Via Calamandrei   N.81</maxs:Address><maxs:ZIPCode>XXXXX</maxs:ZIPCode><maxs:City>XXXXX</maxs:City><maxs:County>AR</maxs:County><maxs:Country>XXXXX</maxs:Country><maxs:Telephone1>0575357716</maxs:Telephone1><maxs:Telephone2></maxs:Telephone2><maxs:Telex></maxs:Telex><maxs:Fax>XXXXXXXX</maxs:Fax><maxs:Internet></maxs:Internet><maxs:EMail>XXXXXX@XXXXXX.XX</maxs:EMail><maxs:ContactPerson>XX. XXXXX</maxs:ContactPerson><maxs:NaturalPerson>false</maxs:NaturalPerson><maxs:Language>IXXT</maxs:Language><maxs:PriceList>XXX</maxs:PriceList><maxs:CustSuppBank></maxs:CustSuppBank><maxs:Payment></maxs:Payment><maxs:CACheck></maxs:CACheck><maxs:IBAN></maxs:IBAN><maxs:IBANIsManual>XXX</maxs:IBANIsManual><maxs:CA></maxs:CA><maxs:CIN></maxs:CIN><maxs:Currency></maxs:Currency><maxs:Notes></maxs:Notes><maxs:CompanyBank></maxs:CompanyBank><maxs:Discount1>-0.0000000</maxs:Discount1><maxs:Discount2>-0.0000000</maxs:Discount2><maxs:DiscountFormula></maxs:DiscountFormula><maxs:ExternalCode>0056.02086</maxs:ExternalCode><maxs:InsertionDate></maxs:InsertionDate><maxs:Region>XXXXXX</maxs:Region><maxs:SkypeID></maxs:SkypeID><maxs:Address2></maxs:Address2><maxs:TBGuid>{2FB2E4DD-5434-4688-B1D5-883D162964EB}</maxs:TBGuid></maxs:CustomersSuppliers><maxs:NaturalPerson/><maxs:Options><maxs:CustSuppType>3211264</maxs:CustSuppType><maxs:Customer>021061</maxs:Customer><maxs:Area></maxs:Area><maxs:Salesperson></maxs:Salesperson><maxs:IsAPrivatePerson>false</maxs:IsAPrivatePerson><maxs:SuspendedTax>false</maxs:SuspendedTax><maxs:TaxCode></maxs:TaxCode><maxs:DebitStampCharges>false</maxs:DebitStampCharges><maxs:DebitCollectionCharges>true</maxs:DebitCollectionCharges><maxs:MaxOrderValue>-0.0000000</maxs:MaxOrderValue><maxs:MaxOrderedValue>-0.0000000</maxs:MaxOrderedValue><maxs:MaximumCredit>-0.0000000</maxs:MaximumCredit><maxs:TBCreated>2008-04-19T10:42:59</maxs:TBCreated><maxs:TBModified>2019-02-05T08:40:27</maxs:TBModified></maxs:Options></maxs:Data></maxs:Customers> ◀
      """
  }
}

这是预期的XML

<?xml version="1.0"?>
<maxs:Customers xmlns:maxs="http://www.microarea.it/Schema/2004/Smart/ERP/CustomersSuppliers/Customers/Users/sa/InfinitySyncroConnector.xsd" tbNamespace="Document.ERP.CustomersSuppliers.Documents.Customers" xTechProfile="InfinitySyncroConnector">
    <maxs:Data>
        <maxs:CustomersSuppliers master="true">
            <maxs:CustSuppType>xxxxx</maxs:CustSuppType>
            <maxs:CustSupp>xxxxx</maxs:CustSupp>
            <maxs:CompanyName>xxxxxxDal xxxxxx&amp; C. S.A.S. (xxxxxx)</maxs:CompanyName>
            <maxs:ISOCountryCode>xxxx</maxs:ISOCountryCode>
            <maxs:TaxIdNumber>xxxxxxx</maxs:TaxIdNumber>
            <maxs:FiscalCode>xx</maxs:FiscalCode>
            <maxs:CustSuppKind>xxxxxx</maxs:CustSuppKind>
            <maxs:Address>Via xxxxxxxN.81</maxs:Address>
            <maxs:ZIPCode>xxxxx</maxs:ZIPCode>
            <maxs:City>xxxxx</maxs:City>
            <maxs:County>AxxR</maxs:County>
            <maxs:Country>xxxx</maxs:Country>
            <maxs:Telephone1>xxxxxxx</maxs:Telephone1>
            <maxs:Telephone2></maxs:Telephone2>
            <maxs:Telex></maxs:Telex>
            <maxs:Fax>xxxxxx</maxs:Fax>
            <maxs:Internet></maxs:Internet>
            <maxs:EMail>xxxxxx@xxxxxxx.it</maxs:EMail>
            <maxs:ContactPerson>xxxx. xxxxx</maxs:ContactPerson>
            <maxs:NaturalPerson>false</maxs:NaturalPerson>
            <maxs:Language>xxx</maxs:Language>
            <maxs:PriceList>xxxx</maxs:PriceList>
            <maxs:CustSuppBank></maxs:CustSuppBank>
            <maxs:Payment></maxs:Payment>
            <maxs:CACheck></maxs:CACheck>
            <maxs:IBAN></maxs:IBAN>
            <maxs:IBANIsManual>false</maxs:IBANIsManual>
            <maxs:CA></maxs:CA>
            <maxs:CIN></maxs:CIN>
            <maxs:Currency></maxs:Currency>
            <maxs:Notes></maxs:Notes>
            <maxs:CompanyBank></maxs:CompanyBank>
            <maxs:Discount1>0.000000000000000</maxs:Discount1>
            <maxs:Discount2>0.000000000000000</maxs:Discount2>
            <maxs:DiscountFormula></maxs:DiscountFormula>
            <maxs:ExternalCode>0056.02086</maxs:ExternalCode>
            <maxs:InsertionDate></maxs:InsertionDate>
            <maxs:Region>Toscana</maxs:Region>
            <maxs:SkypeID></maxs:SkypeID>
            <maxs:Address2></maxs:Address2>
            <maxs:TBGuid>{2FB2E4DD-5434-4688-B1D5-883D162964EB}</maxs:TBGuid>
        </maxs:CustomersSuppliers>
        <maxs:NaturalPerson></maxs:NaturalPerson>
        <maxs:Options>
            <maxs:CustSuppType>3211264</maxs:CustSuppType>
            <maxs:Customer>021061</maxs:Customer>
            <maxs:Area></maxs:Area>
            <maxs:Salesperson></maxs:Salesperson>
            <maxs:IsAPrivatePerson>false</maxs:IsAPrivatePerson>
            <maxs:SuspendedTax>false</maxs:SuspendedTax>
            <maxs:TaxCode></maxs:TaxCode>
            <maxs:DebitStampCharges>false</maxs:DebitStampCharges>
            <maxs:DebitCollectionCharges>true</maxs:DebitCollectionCharges>
            <maxs:MaxOrderValue>0.000000000000000</maxs:MaxOrderValue>
            <maxs:MaxOrderedValue>0.000000000000000</maxs:MaxOrderedValue>
            <maxs:MaximumCredit>0.000000000000000</maxs:MaximumCredit>
            <maxs:TBCreated>2008-04-19T10:42:59</maxs:TBCreated>
            <maxs:TBModified>2019-02-05T08:40:27</maxs:TBModified>
        </maxs:Options>
    </maxs:Data>
</maxs:Customers>

是XML字符串。现在我需要访问这些数据,所以这就是我尝试过的

我尝试过使用simplexml_load_string()simplexml_load_file(),但是由于它是一个很大的xml文件,因此几行后它的读取就停止了。

所以我已经尝试过

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->loadXML($xmlString);
$dom->formatOutput = true;
$dom->saveXML();
print_r($dom);

我得到的输出格式正确,但问题是它无法识别XML的节点。实际上,我无法访问文件的任何属性。

我想访问XML节点。

我是XML的新手,如果问题有一个简单的解决方案,请先抱歉。

预先感谢

1 个答案:

答案 0 :(得分:1)

如果XML如您所显示,请使用SimpleXML加载它。然后,为了帮助命名空间(每个元素的maxs:位),您可以对命名空间($xml->children()部分中的URL)说xmlns:maxs="...。这意味着您可以像访问对象一样访问元素...

$xml = simplexml_load_string($xmlString);
$maxs = $xml->children("http://www.microarea.it/Schema/2004/Smart/ERP/CustomersSuppliers/Customers/Users/sa/InfinitySyncroConnector.xsd");

echo (string)$maxs->Data->CustomersSuppliers->CompanyName.PHP_EOL;
echo (string)$maxs->Data->Options->CustSuppType.PHP_EOL;

给予

xxxxxxDal xxxxxx& C. S.A.S. (xxxxxx)
3211264