【问题标题】:Parsing Microsoft XML in PHP [duplicate]在 PHP 中解析 Microsoft XML [重复]
【发布时间】:2020-09-20 04:14:23
【问题描述】:

我有一个非常简单的 XML 文件(Word 文档的一部分):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="Matter"><vt:lpwstr>30738</vt:lpwstr></property><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="Document number"><vt:lpwstr>999999</vt:lpwstr></property></Properties>

但是,当我使用 SimpleXML 解析它时,我获得了 &lt;property&gt; 项的所有属性,但无法访问这些值(例如,&lt;vt:lpwstr&gt;999999&lt;/vt:lpwstr&gt;)。

$custom = simplexml_load_string($xml);
print_r($custom);

// Results in:
SimpleXMLElement Object
(
    [property] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [fmtid] => {D5CDD505-2E9C-101B-9397-08002B2CF9AE}
                            [pid] => 2
                            [name] => Matter
                        )

                )
            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [fmtid] => {D5CDD505-2E9C-101B-9397-08002B2CF9AE}
                            [pid] => 3
                            [name] => Document number
                        )
                )
        )
)

我错过了什么?

谢谢!

【问题讨论】:

    标签: php xml simplexml docx


    【解决方案1】:

    显然print_r() 在这里不会打印出具有非默认命名空间的 XML 节点,例如您的示例中的 lpwstr

    但是您可以使用 XPath 表达式验证所有节点信息是否可用。三个例子:

    代码:

    <?php
        $xml = <<<XML
        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <Properties 
            xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" 
            xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
                <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="Matter">
                    <vt:lpwstr>30738</vt:lpwstr>
                </property>
                <property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="Document number">
                    <vt:lpwstr>999999</vt:lpwstr>
                </property>
            </Properties>
        XML;
        $xmlobj = new SimpleXMLElement($xml);
        $lpwstr = $xmlobj->xpath('//vt:lpwstr');
        print_r($lpwstr);
        $lpwstr = $xmlobj->xpath('//*[local-name()="lpwstr"]');
        print_r($lpwstr);
        $lpwstr = $xmlobj->xpath('//*');
        print_r($lpwstr);
    ?>
    

    输出:

    Array
    (
        [0] => SimpleXMLElement Object
            (
                [0] => 30738
            )
    
        [1] => SimpleXMLElement Object
            (
                [0] => 999999
            )
    
    )
    Array
    (
        [0] => SimpleXMLElement Object
            (
                [0] => 30738
            )
    
        [1] => SimpleXMLElement Object
            (
                [0] => 999999
            )
    
    )
    Array
    (
        [0] => SimpleXMLElement Object
            (
                [property] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [fmtid] => {D5CDD505-2E9C-101B-9397-08002B2CF9AE}
                                        [pid] => 2
                                        [name] => Matter
                                    )
    
                            )
    
                        [1] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [fmtid] => {D5CDD505-2E9C-101B-9397-08002B2CF9AE}
                                        [pid] => 3
                                        [name] => Document number
                                    )
    
                            )
    
                    )
    
            )
    
        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [fmtid] => {D5CDD505-2E9C-101B-9397-08002B2CF9AE}
                        [pid] => 2
                        [name] => Matter
                    )
    
            )
    
        [2] => SimpleXMLElement Object
            (
                [0] => 30738
            )
    
        [3] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [fmtid] => {D5CDD505-2E9C-101B-9397-08002B2CF9AE}
                        [pid] => 3
                        [name] => Document number
                    )
    
            )
    
        [4] => SimpleXMLElement Object
            (
                [0] => 999999
            )
    
    )
    

    使用 PHP 7.3

    【讨论】:

    • 谢谢,这行得通,但我希望能够以编程方式探索结构,而无需事先知道向 xpath() 提供什么。奇怪的是,“属性”元素似乎没有任何孩子? foreach($custom-&gt;property as $property) { $attr = $property-&gt;attributes(); print "Property '" . $attr['name'] . "' (" . $attr['fmtid'] . "): " . $property-&gt;count() . "\n";-&gt;count() 打印 0,而 foreach($property-&gt;children() as $child) 什么也不做。
    • 也许xpath('//*') 会帮助您理解以编程方式探索的意思。
    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2011-04-07
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    相关资源
    最近更新 更多