SimpleXML Reading node with a hyphenated name

后端 未结 2 522
走了就别回头了
走了就别回头了 2020-11-22 12:18

I have the following XML:




        
2条回答
  •  难免孤独
    2020-11-22 13:02

    Your assumption is correct. Use

    $officeXML->{'document-meta'}
    

    to make it work.

    Please note that the above applies to Element nodes. Attribute nodes (those within the @attributes property when dumping the SimpleXmlElement) do not require any special syntax to be accessed when hyphenated. They are regularly accessible via array notation, e.g.

    $xml = <<< XML
    
        foo
    
    XML;
    $root = new SimpleXMLElement($xml);
    echo $root->{'hyphenated-element'}; // prints "foo"
    echo $root->{'hyphenated-element'}['hyphenated-attribute']; // prints "bar"
    

    See the SimpleXml Basics in the Manual for further examples.

提交回复
热议问题