PHP 使用 simplexml 检索 xml 节点值
可能的重复:
PHP SimpleXML 获取innerXML
我见过其他与 simplexml 和节点值相关的问题,但不能找到我的问题的答案。
我有一个 PHP var,其中包含一个如下形式的 xml 文档:
<div id="container">
<div id="header"></div>
<div id="main">
##CONTENTS## cd <strong>dscsdcsd</strong> sdcsc
<div>my content here</div>
<span>and there</span>
</div>
<div id="footer"> </div>
</div>
我想检索 节点值。但通过以下代码,我还检索了标签:
$xml = simplexml_load_string($_POST['newsletter_body']);
$att = 'id';
foreach ($xml->children() as $el) {
if($el->attributes()->$att == 'main') {
$body_content = $el->asXml();
}
}
有人可以告诉我如何仅获取节点值(不使用xpath)吗?
明确地说,现在我明白了:
<div id="main">
##CONTENTS## cd <strong>dscsdcsd</strong> sdcsc
<div>my content here</div>
<span>and there</span>
</div>
我只想:
##CONTENTS## cd <strong>dscsdcsd</strong> sdcsc
<div>my content here</div>
<span>and there</span>
非常感谢
Possible Duplicate:
PHP SimpleXML get innerXML
I've seen others questions related to simplexml and node value but couldn't find an answer to my problem.
I have a PHP var which contains an xml document formed like that :
<div id="container">
<div id="header"></div>
<div id="main">
##CONTENTS## cd <strong>dscsdcsd</strong> sdcsc
<div>my content here</div>
<span>and there</span>
</div>
<div id="footer"> </div>
</div>
I want to retrieve the node value. But with the following code, I also retrieve the tag :
$xml = simplexml_load_string($_POST['newsletter_body']);
$att = 'id';
foreach ($xml->children() as $el) {
if($el->attributes()->$att == 'main') {
$body_content = $el->asXml();
}
}
Could someone tell me how to get only node value (without using xpath)?
To be clear, now I get :
<div id="main">
##CONTENTS## cd <strong>dscsdcsd</strong> sdcsc
<div>my content here</div>
<span>and there</span>
</div>
And I just want :
##CONTENTS## cd <strong>dscsdcsd</strong> sdcsc
<div>my content here</div>
<span>and there</span>
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @Tomalak 所建议的,这是 Stackoverflow: PHP SimpleXML get innerXML 的重复项
并且您无法比 simplexml API 的建议做得更好。
在您的代码中,将
$body_content = $el->asXml();
替换为$body_content = simplexml_innerXML($el);
但是,您也可以切换到其他 API它区分了
innerXML
(您正在寻找的内容)和outerXML
(您现在获得的内容)。 Microsoft Dom libary 提供了这种区别,但不幸的是 PHP DOM 没有。我发现
PHP XMLReader API
提供了这种区别。请参阅 readInnerXML()。尽管此 API 具有完全不同的处理 XML 的方法。尝试一下。最后,我要强调的是,XML 并不意味着将数据提取为子树,而是提取为值。这就是您在寻找合适的 API 时遇到麻烦的原因。将 HTML 子树存储为值(并转义所有标签)而不是 XML 子树会更“标准”。另请注意,某些 HTML 合成并不总是与 XML 兼容(即
)。无论如何,在实践中,您的方法对于编辑 xml 文件来说肯定更方便。与 ,
As suggested by @Tomalak, this is a duplicate of Stackoverflow: PHP SimpleXML get innerXML
and you cannot do better than suggested with simplexml API.
In your code, replace
$body_content = $el->asXml();
with$body_content = simplexml_innerXML($el);
However, you could also switch to another API that ofers distinction between
innerXML
(what you are looking for) andouterXML
(what you get for now). Microsoft Dom libary offers this distinction but unfortunately PHP DOM doesn't.I found that
PHP XMLReader API
offers this distintion. See readInnerXML(). Though this API has quite a different approach to processing XML. Try it.Finally, I would stress that XML is not meant to extract data as subtrees but rather as value. That's why you running into trouble finding the right API. It would be more 'standard' to store HTML subtree as a value (and escape all tags) rather than XML subtree. Also beware that some HTML synthax are not always XML compatible ( i.e.
<br> vs ,<br/>
). Anyway in practice, you approach is definitely more convenient for editing the xml file.