【问题标题】:PHP: How to extract “content type=”application/xml" nodes from a XML file?PHP:如何从 XML 文件中提取“content type=”application/xml”节点?
【发布时间】:2017-10-24 05:45:10
【问题描述】:

我有一个有效的 XML 文件 (从 SharePoint 生成),它看起来像这样 (在浏览器中)

示例 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://www.example.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    <id>9913f043-xxxx-xxxx-xxxx-xxxx-xxxx</id>
    <title />
    <updated>2017-05-23T06:08:01Z</updated>
    <entry m:etag="&quot;23&quot;">
        <id>Web/Lists(guid'13306095-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx')/Items(1)</id>
        <category term="SP.Data.XXXXXXXXXXXXXXXXXXXXX" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" href="Web/Lists(guid'13306095-xxxx-xxxx-xxxx-xxxx-xxxx')/Items(1)" />
        <title />
        <updated>2017-05-23T06:08:01Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
                <d:Id m:type="Edm.Int32">1</d:Id>
                <d:ContentTypeId>0x0100B6A3B67BE96F724682CCDC8FBE9D70C2</d:ContentTypeId>
                <d:Title m:null="true" />
                <d:Topic>How to google?</d:Topic>
                <d:Cats m:type="Collection(Edm.Int32)">
                    <d:element>1</d:element>
                    <d:element>2</d:element>
                    <d:element>3</d:element>
                    <d:element>4</d:element>
                    <d:element>5</d:element>
                    <d:element>6</d:element>
                    <d:element>7</d:element>
                </d:Cats>
            </m:properties>
        </content>
    </entry>
    <entry>
    .
    .
    </entry>
    <entry>
    .
    .
    </entry>
</feed>

(注意:我这里剪掉了一些重复的节点,因为太长了。)

显然,我们有内部节点&lt;content type="application/xml"&gt;,它也在内部包含数据

问题(使用 PHP 解析时)

在 PHP 中,我使用这段代码来解析(试图提取它):

$xml = simplexml_load_file("data.xml");
foreach ($xml->entry as $item) {
    echo $item->updated . PHP_EOL; // <--- This works!
    print_r($item->content);       // <--- This doesn't work as expected.
}

.. 然后,它给了我这些:

2017-05-23T06:08:01Z
SimpleXMLElement Object
(
  [@attributes] => Array
    (
      [type] => application/xml
    )
)
2017-05-23T06:08:01Z
SimpleXMLElement Object
(
  [@attributes] => Array
    (
      [type] => application/xml
    )
)
.
.

问题(帮助!)

请问我如何提取(获取)那些&lt;content type="application/xml"&gt; 节点中的实际数据?

请帮忙。提前谢谢你。

【问题讨论】:

    标签: php xml content-type application-xml


    【解决方案1】:

    我有一个非常相似的问题。我终于能够让我的示例使用它。

    function pre($array){
        echo "<pre>";
        print_r($array);
        echo "</pre>";
    }
    
    
    $record[$count]['id'] = $id->id;
    $xmlData = utf8_encode(file_get_contents("https://ucf.uscourts.gov/odata.svc/Creditors(guid'81044f71-fb3c-11e5-ac5b-0050569d488e')"));
    $xml = new SimpleXMLElement($xmlData);
    $properties = $xml->content->children('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'); 
    $fields = $properties->properties->children("http://schemas.microsoft.com/ado/2007/08/dataservices"); 
    pre($fields);
    $key = (string)$fields->Key;
    $lastName = (string)$fields->LastName;
    echo $key. "<br />";
    echo $lastName. "<br />";
    

    您需要将 file_get_contents 中的 Url、Key 变量和 LastName 变量替换为您正在寻找的命名空间值,我喜欢使用 pre 函数让事情更容易显示。您可以删除这部分。希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      “内容”下面的元素有一个命名空间(d:...)。不久前我遇到了同样的问题。这应该会有所帮助:

      $xml = simplexml_load_file("data.xml");
      foreach ($xml->entry as $item) {
          echo $item->updated . PHP_EOL;
          $ns = $item->content->children('http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'); 
          print_r($ns->properties); 
      }
      

      我更新了代码。我很确定 print_r($ns->properties) 没有显示完整的子元素……因为它们来自另一个命名空间。我想你可以这样做:

      $nsd = $ns->properties->children("http://schemas.microsoft.com/ado/2007/08/dataservices");
      

      并处理结果。

      在您的示例命名空间中,可以在文档元素中找到:
      xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
      (使用引号之间的 URL)
      d: 和 m: 在文档中用于引用这些命名空间。

      编辑:涉及另一个命名空间。没认出来。该解决方案可以适应。我稍微改了一下代码。

      【讨论】:

      • 我已经编辑了我的问题以提供完整的 XML 结构。请看一下。 (非常感谢您的帮助)
      • 你能告诉我如何选择“命名空间”吗?我在哪里调查?泰。
      • 感谢@Flocke 的帮助。但是我在这个“XML”主题上很愚蠢。你能帮忙把这些放在你上面的答案中吗? (我按我的理解试了,还是不行。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多