PHP simpleXML根据值获取不同节点的值
我有以下XML代码:PHP simpleXML根据值获取不同节点的值,php,xml,simplexml,Php,Xml,Simplexml,我有以下XML代码: <administration> <notes> <note> <id>12312312</id> <name>Lorem Ipsum</name> <reference>Target Value - 1</reference> </note>
<administration>
<notes>
<note>
<id>12312312</id>
<name>Lorem Ipsum</name>
<reference>Target Value - 1</reference>
</note>
<note>
<id>12312365</id>
<name>Lorem Ipsum</name>
<references>
<code>Dolor it se met.</code>
<code>Target Value - 2</code>
</references>
</note>
<note>
<id>12375512</id>
<name>Target Value - 3</name>
<reference>S</reference>
</note>
</notes>
<accounting>
<ledgers>
<ledger>
<debits>
<debit>
<description>Target Value - 4</description>
<amount>5467.32</amount>
</debit>
<debit>
<description>My Debit</description>
<amount>5467.32</amount>
<tags>
<tag>Target Value - 5</tag>
</tags>
</debit>
</debits>
<credits>
<credit>
<title>Target Value - 6</title>
<amount>873.00</amount>
</credit>
<credit>
<description>Target Value - 7</description>
<amount>23454.12</amount>
</credit>
</credits>
</ledger>
</ledgers>
</accounting>
</administration>
我试图得到一个PHP数组,它只包含节点的值,节点的值包含以下字符串:“Target value”。
这必须以递归的方式完成,使用XML解析器(我正在尝试SimpleXML,但我是新手)
到目前为止,我一直在尝试使用SimpleXmlIterator和foreach以及for循环来实现这一点,但我似乎无法检查节点值是否包含“目标值”
编辑:通过手动引用来到达目标节点不是我想要的,如果是,就不会有问题
有没有办法做到这一点
编辑:
以下是我最后一次尝试的代码:
function sxiToArray($sxi)
{
$a = array();
for( $sxi->rewind(); $sxi->valid(); $sxi->next() )
{
if(!array_key_exists($sxi->key(), $a))
{
$a[$sxi->key()] = array();
}
if($sxi->hasChildren())
{
if (strpos((string)$sxi->current(), "Target Value"))
$a[$sxi->key()][] = sxiToArray($sxi->current());
}
else
{
if (strpos((string)$sxi->current(), "Target Value"))
$a[$sxi->key()][] = strval($sxi->current());
}
}
return $a;
}
$xmlArray = xml2array('../Document.xml');
print_r($xmlArray);
这将在运行后给出以下结果:
数组([notes]=>Array()[accounting]=>Array())为什么不试试str_pos()作为“目标值”?我不知道如何迭代XML,但可以执行以下操作:
if(str_pos($node, "Target value"){
//do whatever
}
这将告诉您是否有任何节点至少包含该特定字符串 为什么不试试str_pos()作为“目标值”?我不知道如何迭代XML,但可以执行以下操作:
if(str_pos($node, "Target value"){
//do whatever
}
这将告诉您是否有任何节点至少包含该特定字符串 它不必以递归方式完成。您可以使用Xpath。Xpath使用位置路径作为表达式的一部分。路径使用不同的轴-其中一个是后代。它“忽略”嵌套。Xpath允许您使用条件
- 获取文档中的任何元素节点
/*
- 将文本节点作为子节点的
/*[./text()]
- 文本节点包含字符串“Target Value”
/*[./text()[包含(,“目标值”)]
$administration = new SimpleXMLElement($xml);
$nodes = $administration->xpath('//*[./text()[contains(., "Target Value")]]');
foreach ($nodes as $node) {
var_dump($node->getName(), (string)$node);
}
输出:
string(9) "reference"
string(16) "Target Value - 1"
string(4) "code"
string(16) "Target Value - 2"
string(4) "name"
string(16) "Target Value - 3"
string(11) "description"
string(16) "Target Value - 4"
string(3) "tag"
string(16) "Target Value - 5"
string(5) "title"
string(16) "Target Value - 6"
string(11) "description"
string(16) "Target Value - 7"
对于DOM,它看起来不会有太大的不同:
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$nodes = $xpath->evaluate('//*[./text()[contains(., "Target Value")]]');
foreach ($nodes as $node) {
var_dump($node->localName, $node->textContent);
}
它不必以递归的方式完成。您可以使用Xpath。Xpath使用位置路径作为表达式的一部分。路径使用不同的轴-其中一个是后代。它“忽略”嵌套。Xpath允许您使用条件
- 获取文档中的任何元素节点
/*
- 将文本节点作为子节点的
/*[./text()]
- 文本节点包含字符串“Target Value”
/*[./text()[包含(,“目标值”)]
$administration = new SimpleXMLElement($xml);
$nodes = $administration->xpath('//*[./text()[contains(., "Target Value")]]');
foreach ($nodes as $node) {
var_dump($node->getName(), (string)$node);
}
输出:
string(9) "reference"
string(16) "Target Value - 1"
string(4) "code"
string(16) "Target Value - 2"
string(4) "name"
string(16) "Target Value - 3"
string(11) "description"
string(16) "Target Value - 4"
string(3) "tag"
string(16) "Target Value - 5"
string(5) "title"
string(16) "Target Value - 6"
string(11) "description"
string(16) "Target Value - 7"
对于DOM,它看起来不会有太大的不同:
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$nodes = $xpath->evaluate('//*[./text()[contains(., "Target Value")]]');
foreach ($nodes as $node) {
var_dump($node->localName, $node->textContent);
}
你能发布你用SimpleXmlIterator尝试过的代码吗?如果你不想使用循环或比较值,为什么不使用正则表达式?@khuderm使用正则表达式的确切含义是什么?我应该用regex比较材料检查每个节点吗?你能发布你用SimpleXmlIterator尝试过的代码吗?如果你不想使用循环或比较值,为什么不使用regex?@khuderm使用regex到底是什么意思?我应该用正则表达式比较材料检查每个节点吗?我已经做了类似的事情,但是我的作业特别提到了递归方式的使用……我已经做了类似的事情,但是我的作业特别提到了递归方式的使用……谢谢,这正是我要找的。谢谢,这正是我要找的。