如何获取 SimpleXmlElement 属性的值?

我有这个 XML 代码:


<w:footerReference w:type="default" r:id="rId6"/>

在 PHP 我有这个代码:


// $footer is a SimpleXMLElement, contain the code above


foreach ($footer->attributes() as $attr_name => $attr_value) {

    dd($attr_name." = ".$attr_value);

}

并且foreach没有运行。


我也试过这个:


$type = 'type';

$footer->attributes()->$type; // empty string


$wtype = 'w:type';

$footer->attributes()->$wtype; // empty string

当然,我可以将 XML 转换为字符串并执行一些正则表达式魔术,但在我看来这不是一个好方法。


更新:


这是整个 XML 文档代码:


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" 

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

    xmlns:o="urn:schemas-microsoft-com:office:office" 

    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" 

    xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" 

    xmlns:v="urn:schemas-microsoft-com:vml" 

    xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" 

    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" 

    xmlns:w10="urn:schemas-microsoft-com:office:word" 

    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" 

    xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" 

    xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" 

    xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" 

    xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" 

    xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" 

    xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">

    <w:body>

        <w:sectPr w:rsidR="00654EDA">

            <w:footerReference w:type="default" r:id="rId6"/>

        </w:sectPr>

    </w:body>

</w:document>

如何访问w:type和r:id属性值?


千巷猫影
浏览 268回答 1
1回答

ITMISS

您必须将属性命名空间作为参数传递 attributes$type = $footer->attributes("http://schemas.openxmlformats.org/wordprocessingml/2006/main")->type;$id = $footer->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships")->id;与 foreach 相同foreach ($footer->attributes("http://schemas.openxmlformats.org/wordprocessingml/2006/main") as $attr_name => $attr_value) {&nbsp; &nbsp; dd($attr_name." = ".$attr_value);}foreach ($footer->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships") as $attr_name => $attr_value) {&nbsp; &nbsp; dd($attr_name." = ".$attr_value);}
打开App,查看更多内容
随时随地看视频慕课网APP