Php 如何在启动时使用DomNode获取属性值
我希望不使用foreach直接获取特定属性的值,例如 我的代码:Php 如何在启动时使用DomNode获取属性值,php,xpath,attributes,dom-node,Php,Xpath,Attributes,Dom Node,我希望不使用foreach直接获取特定属性的值,例如 我的代码: $xmldoc = new DOMDocument; $xmldoc->loadXML($this->Stream); $xpa = new DOMXPath($xmldoc); $titles = $xpa->query("//tpd:ParameterSet[@ProcessUsage='Customer']/tpd:Parameter/tpd:CustomerInfo");
$xmldoc = new DOMDocument;
$xmldoc->loadXML($this->Stream);
$xpa = new DOMXPath($xmldoc);
$titles = $xpa->query("//tpd:ParameterSet[@ProcessUsage='Customer']/tpd:Parameter/tpd:CustomerInfo");
foreach ($titles as $title) {
if ($title->hasAttributes()) {
foreach ($title->attributes as $attribute) {
print "AttributName= " . $attribute->name . "\n";
print "AttributValue= " . $attribute->value . "\n";
}
}
"T:\Program\PHP\php.exe" T:\run.php
AttributName= CustomerID
AttributValue= 1223
AttributName= CustomerOrderID
AttributValue= 1552045326210
Process finished with exit code 0
到目前为止,它将为您提供输出:
$xmldoc = new DOMDocument;
$xmldoc->loadXML($this->Stream);
$xpa = new DOMXPath($xmldoc);
$titles = $xpa->query("//tpd:ParameterSet[@ProcessUsage='Customer']/tpd:Parameter/tpd:CustomerInfo");
foreach ($titles as $title) {
if ($title->hasAttributes()) {
foreach ($title->attributes as $attribute) {
print "AttributName= " . $attribute->name . "\n";
print "AttributValue= " . $attribute->value . "\n";
}
}
"T:\Program\PHP\php.exe" T:\run.php
AttributName= CustomerID
AttributValue= 1223
AttributName= CustomerOrderID
AttributValue= 1552045326210
Process finished with exit code 0
我的问题:
是否可以直接获取tpd:CustomerInfo/CustomerOrderID的属性值
谢谢你的帮助 直接获取属性的XPath是:
//tpd:ParameterSet[@ProcessUsage='Customer']/tpd:Parameter/tpd:CustomerInfo/@CustomerOrderID
好的,我如何在php中输出该值?