如何使用xpath从节点内部获取文本?
现在我这样做:
$temp= $content->xpath('qwe/qwe');
$temp = each($temp[0]);
return $temp['value'];
Run Code Online (Sandbox Code Playgroud)
但正如你可以看到它远没有很好的解决方案:(
在c#中它很简单
public string readXmlVar(string xpath)
{
XmlNode xmlNode = xml.SelectSingleNode(xpath);
return xmlNode.InnerText;
}
Run Code Online (Sandbox Code Playgroud)
尝试:
$dom = new DOMDocument;
$dom->loadXML('<root>
<p>some text<br />some more text</p>
</root>');
$xpath = new DOMXPath($dom);
foreach ($xpath->query('/root/p/text()') as $textNode) {
echo $textNode->nodeValue;
}
Run Code Online (Sandbox Code Playgroud)
$textNode将是一个DOMText.
对于SimleXmlElement只投它作为一个字符串:return (string) $temp;.供DomDocument使用return $temp->nodeValue.
| 归档时间: |
|
| 查看次数: |
16357 次 |
| 最近记录: |