Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP解析Microsoft XML_Php_Xml_Simplexml_Docx - Fatal编程技术网

用PHP解析Microsoft XML

用PHP解析Microsoft XML,php,xml,simplexml,docx,Php,Xml,Simplexml,Docx,我有一个非常简单的XML文件(Word文档的一部分): <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://sch

我有一个非常简单的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>
我错过了什么

谢谢

显然
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
        )

)

感谢使用PHP7.3,这是可行的,但我希望能够通过编程探索该结构,而不必事先知道向xpath()提供什么。奇怪的是,“属性”元素似乎没有任何子元素
foreach($custom->property as$property){$attr=$property->attributes();打印“property”.$attr['name'].“($attr['fmtid'.]”:“$property->count()。“\n”
->count()
打印0,而
foreach($property->children()as$child)
什么都不做。也许
xpath('/*)
将有助于理解您所说的以编程方式探索的含义。
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
        )

)