如何使用xpath从节点内部获取文本?
现在我是这样做的:
$temp= $content->xpath('qwe/qwe');
$temp = each($temp[0]);
return $temp['value'];
但正如你所看到的,这远不是一个好的解决方案:(
在c#中,它就像一样简单
public string readXmlVar(string xpath)
{
XmlNode xmlNode = xml.SelectSingleNode(xpath);
return xmlNode.InnerText;
}
对于SimleXmlElement
,只需将其强制转换为字符串:return (string) $temp;
。对于DomDocument
,请使用return $temp->nodeValue
。
尝试:
$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;
}
$textNode
将是DOMText。
(string) current($xml->xpath("//group[@ref='HS_TYP']"))
希望对有所帮助
这将从简单的xml元素中获取所有内部文本
dom_import_simplexml($xpath->query('//div[@class="h1"]')->textContent;