【问题标题】:Accessing XML content via PHP using SimpleXML使用 SimpleXML 通过 PHP 访问 XML 内容
【发布时间】:2010-07-19 11:39:54
【问题描述】:

我正在使用下面的 XML 文件字符串,并尝试了多种方法来尝试访问 XML 内容的某些部分。请参阅下面 XML 文件后的代码以供我尝试:

<?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">
<soap:Body>
<Address_ListResponse xmlns="http://example.example.com/">
<Address_ListResult>
<Address>
<HoldingId xsi:nil="true"/>
<MainId>1617931</MainId>
<ContactId>8</ContactId>
<Description>Home, All Purposes</Description>
<Position/>
<Department/>
<Organisation/>
<AddressLabel>Mr Joe Bloggs</AddressLabel>
<AddressLine1>1 Fake Road</AddressLine1>
<AddressLine2/>
<AddressLine3/>
<Town>Faketown</Town>
<CountyId>818</CountyId>
<PostCode>FA33 4KE</PostCode>
<CountryId>3</CountryId>
<Phone>01234567890</Phone>
<EvePhone/>
<Mobile/>
<Email>joe@bloggs.com</Email>
<Fax/>
<WWW/>
<AddressTypeId>1</AddressTypeId>
<IsBilling>true</IsBilling>
<IsMailing>true</IsMailing>
<IsDelivery>true</IsDelivery>
<IsInherited>false</IsInherited>
<GridN/>
<GridE/>
<Latitude/>
<Longitude/>
<CensationCode/>
<IsDeleted>false</IsDeleted>
<HoldingPersonalDetailsId xsi:nil="true"/>
<IsSynced>false</IsSynced>
<BeenProcessed>false</BeenProcessed>
<CountyName/>
<CountryName/>
<AddressTypeName>Home</AddressTypeName>
</Address>
</Address_ListResult>
</Address_ListResponse>
</soap:Body>
</soap:Envelope>

访问 XML 内容的代码:

$xml = simplexml_load_string($result);

echo "Town: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->children('http://example.example.com/')->Address_ListResponse->Town;

以上代码基于另一个 StackOverFlow 问题发布的链接:http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: php xml simplexml


    【解决方案1】:

    考虑改用SOAP extension
    请参阅the example in the PHP Manual 了解如何编写客户端。

    另一种方法是将Zend_Soap 用作独立组件。

    【讨论】:

    • 这些链接似乎都是关于使用 SOAP 获取数据的。我实际上已经获得了 XML 数据,现在想使用 PHP 来操作它。我看不出 SOAP 扩展如何帮助我做到这一点?
    • @baswoni Soap 客户端可以从 Soap 服务器获取数据。如果您在创建 Soap 客户端时指定类映射,则可以将返回的数据结构映射到您的类。请参阅示例中的最后四行。
    【解决方案2】:

    原来我正在寻找的答案不是 SimpleXML - 或者至少我无法让它工作。

    我所做的是使用 xml_parse_into_struct 创建从 XML 数据返回的值数组:http://www.php.net/manual/en/function.xml-parse-into-struct.php

    【讨论】:

      猜你喜欢
      • 2012-08-06
      • 2017-05-17
      • 1970-01-01
      • 2012-04-13
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      相关资源
      最近更新 更多