使用PHP按ID获取XML元素

使用PHP按ID获取XML元素,php,xml,Php,Xml,我在让PHP读取XML文档时遇到了一些困难。我试图根据我选择的catid从每个节点回显内容 XML:text.XML <root> <category catid='1'> <text id='TXT1'><![CDATA[ Lorem Ipsum ]]></text> <text id='TXT2'><![CDATA[ Lorem Ipsum ]]></text> <

我在让PHP读取XML文档时遇到了一些困难。我试图根据我选择的catid从每个节点回显内容

XML:text.XML

<root>
  <category catid='1'>
    <text id='TXT1'><![CDATA[ Lorem Ipsum ]]></text>
    <text id='TXT2'><![CDATA[ Lorem Ipsum ]]></text>
    <text id='TXT3'><![CDATA[ Lorem Ipsum ]]></text>
  </category>
  <category catid='2'>
    <text id='TXT1'><![CDATA[ Lorem Ipsum ]]></text>
    <text id='TXT2'><![CDATA[ Lorem Ipsum ]]></text>
    <text id='TXT3'><![CDATA[ Lorem Ipsum ]]></text>
  </category>
</root>

PHP:


您可以使用DOMDocument::getElementById了解更多信息,请参见。

您可以使用DOMDocument::getElementById了解更多信息。

下面是一个示例,说明如何使用DOM扩展和XPATH来完成此操作:

$doc = new DOMDocument();
$doc->loadXML($xml);

$selector = new DOMXPath($doc);

$result = $selector->query("//category[@catid='1']");
if($result->length !== 1) {
    die('BAD xml');
}

$category = $result->item(0);
$ids = array('TXT1', 'TXT2', 'TXT3');

foreach($ids as $id){
    // note $category as the second argument. meaning that the query
    // is relative to the category node and not to the root node
    $textResult = $selector->query("text[@id='$id']", $category);
    if($textResult->length < 1) {
        die('BAD xml');
    }

    $text = $textResult->item(0)->nodeValue;
    echo $text, PHP_EOL;
}
$doc=newDOMDocument();
$doc->loadXML($xml);
$selector=newdomxpath($doc);
$result=$selector->query(//category[@catid='1']);
如果($result->length!==1){
die('badxml');
}
$category=$result->item(0);
$ids=数组('TXT1','TXT2','TXT3');
foreach($id作为$id){
//注意$category作为第二个参数
//相对于类别节点,而不是根节点
$textResult=$selector->query(“text[@id='$id']”,$category);
如果($textResult->长度<1){
die('badxml');
}
$text=$textResult->item(0)->nodeValue;
echo$text,PHP_EOL;
}

下面是一个如何将DOM扩展与XPATH结合使用的示例:

$doc = new DOMDocument();
$doc->loadXML($xml);

$selector = new DOMXPath($doc);

$result = $selector->query("//category[@catid='1']");
if($result->length !== 1) {
    die('BAD xml');
}

$category = $result->item(0);
$ids = array('TXT1', 'TXT2', 'TXT3');

foreach($ids as $id){
    // note $category as the second argument. meaning that the query
    // is relative to the category node and not to the root node
    $textResult = $selector->query("text[@id='$id']", $category);
    if($textResult->length < 1) {
        die('BAD xml');
    }

    $text = $textResult->item(0)->nodeValue;
    echo $text, PHP_EOL;
}
$doc=newDOMDocument();
$doc->loadXML($xml);
$selector=newdomxpath($doc);
$result=$selector->query(//category[@catid='1']);
如果($result->length!==1){
die('badxml');
}
$category=$result->item(0);
$ids=数组('TXT1','TXT2','TXT3');
foreach($id作为$id){
//注意$category作为第二个参数
//相对于类别节点,而不是根节点
$textResult=$selector->query(“text[@id='$id']”,$category);
如果($textResult->长度<1){
die('badxml');
}
$text=$textResult->item(0)->nodeValue;
echo$text,PHP_EOL;
}

请注意,这也可以使用简单的xml。我手边根本没有榜样。如果我有时间,也许我会准备一个完整的。再见!:)请注意,这也应该可以使用简单的xml。我手边根本没有榜样。如果我有时间,也许我会准备一个完整的。再见!:)您可以对
@id=
执行与第二行代码中的
@catid=
完全相同的操作。有什么理由不尝试吗?您可以对
@id=
执行与第二行代码中的
@catid=
完全相同的操作。你为什么不试试?