SimpleXMLElement对象解析

时间:2016-08-25 18:34:02

标签: php xml

我有xml文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<comments
    xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <authors>
        <author></author>
    </authors>
    <commentList>
        <comment authorId="0" ref="G2">
            <text>
                <t xml:space="preserve"> test comment 1</t>
            </text>
        </comment>
        <comment authorId="0" ref="G4">
            <text>
                <t xml:space="preserve"> test comment 2</t>
            </text>
        </comment>
    </commentList>
</comments>

我通过SimpleXMLElement php函数加载它

$com = new SimpleXMLElement('data.xml');

当我print_r($ com)我得到

SimpleXMLElement Object
(
    [authors] => SimpleXMLElement Object
        (
            [0] => SimpleXMLElement Object
                ()
        )
    [commentList] => SimpleXMLElement Object
        (
            [comment] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [authorId] => 0
                                    [ref] => G2
                                )
                            [text] => SimpleXMLElement Object
                                ([t] => test comment 1)
                        )
                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [authorId] => 0
                                    [ref] => G4
                                )
                            [text] => SimpleXMLElement Object
                                (
                                    [t] => test comment 2
                                )
                        )
                )
        )
) 

我想使用ref值读取注释,我试图读取G2的值,如

echo $value = (string) $comments->commentList->comment[0]->ref['G2'];

但没有成功,任何想法如何通过参考值读取值,如G2,G4

2 个答案:

答案 0 :(得分:1)

如果您想获得与ref="G2"的任何评论相关的评论文字,那么您就可以执行此操作。遍历每个注释元素,获取ref属性的值,根据目标值进行检查,如果匹配,则获取文本值。

$xmlElement = new SimpleXMLElement($myXML);

foreach($xmlElement->commentList->comment as $comment)
{
    if($comment["ref"] == "G2")
    {
        echo $comment->text->t;
        // optionally echo line break or some other formatting
    }
}

Live Example

答案 1 :(得分:0)

您可以使用Xpath获取由属性过滤的评论文本:

$comments = new SimpleXMLElement($xml);
$comments->registerXpathNamespace(
  'ofm', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
);

$texts = $comments->xpath('ofm:commentList/ofm:comment[@ref="G2"]/ofm:text');
foreach ($texts as $text) {
  var_dump((string)$text->t);
}

输出:

string(15) " test comment 1"

您的XML使用默认命名空间,因此对于Xpath,您必须注册并使用前缀。

DOM不会有太大的不同:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace(
  'ofm', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
);

$texts = $xpath->evaluate(
  '/ofm:comments/ofm:commentList/ofm:comment[@ref="G2"]/ofm:text'
);
foreach ($texts as $text) {
  var_dump($text->textContent);
}