为了账号安全,请及时绑定邮箱和手机立即绑定

如何获取 SimpleXmlElement 属性的值?

如何获取 SimpleXmlElement 属性的值?

PHP
千巷猫影 2021-12-03 19:02:19
我有这个 XML 代码:<w:footerReference w:type="default" r:id="rId6"/>在 PHP 我有这个代码:// $footer is a SimpleXMLElement, contain the code aboveforeach ($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属性值?
查看完整描述

1 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

您必须将属性命名空间作为参数传递 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) {

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

}

foreach ($footer->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships") as $attr_name => $attr_value) {

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

}


查看完整回答
反对 回复 2021-12-03
  • 1 回答
  • 0 关注
  • 267 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信