【问题标题】:PHP parsing xml issues with get valuesPHP解析xml问题与获取值
【发布时间】:2014-07-24 06:07:44
【问题描述】:

我以 xml 格式接收文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document xmlns="http://adress1" xmlns:adr="http://adress2" xmlns:inst="adress3" xmlns:meta="adress4" xmlns:oso="adress5" xmlns:str="adress6" xmlns:xsi="adress7">
    <str:DataDocument>
        <str:Head/>
        <meta:Date typeDate="created">
             <meta:Time>2014-07-23T12:35:20+02:00</meta:Time>
        </meta:Date>
    </str:DataDocument>
    <contentDocument format="text/xml" coding="xml">
        <Values>
            <Attachments>
                  <str:Attachment format="text/html" code="base64" nameFile="name.html">
                       <str:DataAttachment>VALUESRECEIVE</str:DataAttachment>
                   </str:Attachment>
                   <str:Attachment format="text/xml" code="base64" nameFile="name.xml">
                       <str:DataAttachment>VALUESToRECEIVE</str:DataAttachment>
                   </str:Attachment>
                   <str:Attachment format="text/xml" code="base64" nameFile="name2.xml">
                       <str:DataAttachment>VALUESToRECEIVE</str:DataAttachment>
                   </str:Attachment>
             </Attachments>
         </Values>
    </contentDocument>
    (...)
</Document>

我必须接收所有节点:&lt;str:DataAttachment&gt;&lt;str:DataAttachment&gt; 对于每个 &lt;str:Attachment&gt;

这是我写的:

$attachment = new SimpleXMLElement(file_get_contents($file1));
//first way
$res = $attachment->xpath('contentDocument/Values/Attachments/*');
//second way            
$zalacznikiListFromXml = $attachment->contentDocument->Values->Attachments;
foreach ($attachmentListFromXml as $Attachments){
    foreach($Attachmentsas $strAttachment)
        $attachToDecode = $strAttachment['str:DataAttachment'];
}

但是xpath$attachment-&gt;contentDocument-&gt;Values-&gt;Attachments 都返回空对象。

我不知道是什么问题。你能帮我找到每个 str:DataAttachment 吗?

感谢您的帮助。

【问题讨论】:

    标签: php xml parsing xpath


    【解决方案1】:

    详细说明@Ghost 的答案...

    您的“第一种方法”不起作用有几个原因。

    1. 输入 XML 中的大多数元素都位于默认命名空间中,即 URI 为 "http://adress1" 的命名空间。这是因为最外层元素具有默认命名空间声明xmlns="http://adress1"。所以这个默认命名空间被所有没有明确命名空间前缀的元素继承。因此,为了在 XPath 中选择这些元素,您必须告诉 XPath 您想要名称空间中的元素,其 URI 为 "http://adress1"。 Ghost 展示了如何声明名称空间前缀并在 XPath 中使用它。对于adress1 命名空间,您可以使用$attachment-&gt;registerXPathNamespace('ns1', 'http://adress1');

    2. 其次,$attachment-&gt;xpath('contentDocument/...') 与输入文档的结构不匹配。 $attachment 保存输入文档的根节点,它是&lt;Document&gt; 的不可见父节点。然后,您尝试选择名为contentDocument 的根节点的子节点。但是&lt;contentDocument&gt;&lt;Document&gt; 的子节点,而不是根节点的子节点。所以你需要像$attachment-&gt;xpath('/*/ns1:contentDocument/ns1:Values/ns1:Attachments/*');

    3. 这样的东西

    【讨论】:

    • +1 以获得全面的解释。也帮助了我
    • 谢谢!它也帮助了我
    【解决方案2】:

    如果您选择使用 xpath,则需要先注册命名空间。使用registerXPathNamespace

    例子:

    $attachToDecode = array();
    $attachment = new SimpleXMLElement(file_get_contents($file1));
    $attachment->registerXPathNamespace('str', 'adress6');
    foreach($attachment->xpath('//str:DataAttachment') as $strAttachment) {
        $attachToDecode[] = (string) $strAttachment;
    }
    
    echo '<pre>';
    print_r($attachToDecode);
    

    样本输出:

    VALUESRECEIVE
    VALUESToRECEIVE
    VALUESToRECEIVE
    

    【讨论】:

      猜你喜欢
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      相关资源
      最近更新 更多