无法在XML中获取子元素值

时间:2017-05-08 03:00:56

标签: php xml web-services domdocument

我从Web服务获得以下响应。我需要获取dtAccData(<form class="form" action="mailto:someone@example.com"> FULL_NAME

中的值
GLOBAL_ID

如果我加载到简单的XML对象并转储,我在dtAccData中看不到任何值。我只能到达下面。

    <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <strGetCustomerValidationResponse xmlns="http://tempuri.org/">
    <strGetCustomerValidationResult>21^Please complete your pending form^150675^1</strGetCustomerValidationResult>
    <dtAccData>
      <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:MainDataTable="Table" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="FULL_NAME" type="xs:string" minOccurs="0" />
                <xs:element name="GLOBAL_ID" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
            </xs:element>
          </xs:choice>
        </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">
        <NewDataSet xmlns="">
          <Table diffgr:id="Table1" msdata:rowOrder="0">
            <FULL_NAME>TEST39</FULL_NAME>
            <GLOBAL_ID>37405-9879863-7</GLOBAL_ID>
          </Table>
        </NewDataSet>
      </diffgr:diffgram>
    </dtAccData>
  </strGetCustomerValidationResponse>
</soap:Envelope>

我得到了

$xml = new SimpleXMLElement($xml_tring);
var_dump($xml);

我该怎么做。

1 个答案:

答案 0 :(得分:1)

我们在这里使用DOMDocument来提取元素中的textContent

Try this code snippet here

<?php

ini_set('display_errors', 1);
$string='<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <strGetCustomerValidationResponse xmlns="http://tempuri.org/">
    <strGetCustomerValidationResult>21^Please complete your pending form^150675^1</strGetCustomerValidationResult>
    <dtAccData>
      <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:MainDataTable="Table" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="FULL_NAME" type="xs:string" minOccurs="0" />
                <xs:element name="GLOBAL_ID" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
            </xs:element>
          </xs:choice>
        </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">
        <NewDataSet xmlns="">
          <Table diffgr:id="Table1" msdata:rowOrder="0">
            <FULL_NAME>TEST39</FULL_NAME>
            <GLOBAL_ID>37405-9879863-7</GLOBAL_ID>
          </Table>
        </NewDataSet>
      </diffgr:diffgram>
    </dtAccData>
  </strGetCustomerValidationResponse>
</soap:Envelope>';
$domDocument= new DOMDocument();
$domDocument->loadXML($string);
echo $FULL_NAME=$domDocument->getElementsByTagName("FULL_NAME")->item(0)->textContent;
echo PHP_EOL;
echo $GLOBAL_ID=$domDocument->getElementsByTagName("GLOBAL_ID")->item(0)->textContent;

<强>输出:

TEST39 37405-9879863-7

如果您的XML回复中包含否。您要从中提取信息的这些标记。您可以使用此代码获取multiple occurences

Try this code snippet here for multiple occurences

<?php

$domDocument= new DOMDocument();
$domDocument->loadXML($string);
$result=array();
foreach($domDocument->getElementsByTagName("FULL_NAME") as $fullnameElement)
{
    $result["FULL_NAME"][]=$fullnameElement->textContent;
}
foreach($domDocument->getElementsByTagName("GLOBAL_ID") as $fullnameElement)
{
    $result["GLOBAL_ID"][]=$fullnameElement->textContent;
}
print_r($result);

<强>输出:

Array
(
    [FULL_NAME] => Array
        (
            [0] => TEST39
        )

    [GLOBAL_ID] => Array
        (
            [0] => 37405-9879863-7
        )

)
相关问题