How how can I get a text from inside a node using xpath?
如何使用xpath从节点内部获取文本?
For now I'm doing it like this:
现在我这样做:
$temp= $content->xpath('qwe/qwe');
$temp = each($temp[0]);
return $temp['value'];
But as you can see it's far from good solution :(
但正如你可以看到它远没有很好的解决方案:(
In c# it is as easy as
在c#中它很简单
public string readXmlVar(string xpath)
{
XmlNode xmlNode = xml.SelectSingleNode(xpath);
return xmlNode.InnerText;
}
4 个解决方案
#1
7
For SimleXmlElement
just cast it as a string: return (string) $temp;
. For DomDocument
use return $temp->nodeValue
.
对于SimleXmlElement,只需将其转换为字符串:return(string)$ temp;。对于DomDocument,请使用return $ temp-> nodeValue。
#2
6
Try:
尝试:
$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
will be a DOMText.
$ textNode将是一个DOMText。
#3
2
(string) current($xml->xpath("//group[@ref='HS_TYP']"))
Hope that helps
希望有所帮助
#4
1
This will get all inner text from simple xml element
这将从简单的xml元素中获取所有内部文本
dom_import_simplexml($xpath->query('//div[@class="h1"]')->textContent;
#1
7
For SimleXmlElement
just cast it as a string: return (string) $temp;
. For DomDocument
use return $temp->nodeValue
.
对于SimleXmlElement,只需将其转换为字符串:return(string)$ temp;。对于DomDocument,请使用return $ temp-> nodeValue。
#2
6
Try:
尝试:
$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
will be a DOMText.
$ textNode将是一个DOMText。
#3
2
(string) current($xml->xpath("//group[@ref='HS_TYP']"))
Hope that helps
希望有所帮助
#4
1
This will get all inner text from simple xml element
这将从简单的xml元素中获取所有内部文本
dom_import_simplexml($xpath->query('//div[@class="h1"]')->textContent;