【问题标题】:Return parent node after string search in XML - PHP在 XML 中搜索字符串后返回父节点 - PHP
【发布时间】:2015-04-14 06:21:08
【问题描述】:

我有一个这样的 XML 文件:

<GenResponse>
  <Detail1></Detail1>
  <Detail2></Detail>
  <DataNodes>
    <DataNode>
      <NodeDetails1>
        <node4>Parrot Musky Truck Moo</node4>
        <node5>Tinker Singer Happy Fool</node5>
        <node6>
          <FurtherDetails>
            <Node>Musky</Node>
            <Node>Lorem Ipsum</Node>
          </FurtherDetails>
      </NodeDetails1>
      <NodeDetails2>ID</NodeDetails2>
    </DataNode>
    <DataNode>
      <NodeDetails1>
        <node4>Sky Star Panet Shoe</node4>
        <node5>Rusky Husky Musky Boo</node5>
      </NodeDetails1>
      <NodeDetails2>ID</NodeDetails2>
    </DataNode>
  </DataNodes>
</GenResponse>

我想知道如何将搜索字符串“Musky”注入 PHP 函数并返回 &lt;DataNode&gt;...&lt;/DataNode&gt;&lt;DataNode&gt;...&lt;/DataNode&gt;

基本上我想在一个巨大的 XML 文件中搜索一个字符串并返回所有包含该字符串的 DataNode。

如果 SimpleXML 可以做到这一点,那就太好了。否则任何其他解决方案也可以。

编辑:注意“Musky”如何出现在&lt;DataNode&gt; 下的不同节点中

【问题讨论】:

  • 如果你还没有开始编码,你可以通过xpathtuxradar.com/practicalphp/12/3/3搜索节点
  • 你确定你的话会一直在node2中
  • @SunilPachlangia 是的。 XML 将包含 100 个这样的 node2,搜索字符串将落在 node2 内的任何位置。谢谢
  • @jQuery.PHP.Magento.com 我正在检查答案.. 非常感谢

标签: php xml codeigniter simplexml


【解决方案1】:

使用

$xmlStr = file_get_contents('data/your_XML_File.xml'); 
$xml = new SimpleXMLElement($xmlStr); 
// seach records by tag value: 
// find nodes with  text
$res = $xml->xpath("node2[contains(., 'Musky')]"); 
print_r($res); 

//为了测试目的只是在编辑器中复制粘贴以下代码,为了测试,我没有使用单独的xml文件。

<?php
//$xmlStr = file_get_contents('test.xml'); 
$xmlStr = '<node1>
  <node2>
    <node3>
      <node4>Parrot Singer Truck Moo</node4>
      <node5>Tinker Musky Happy Fool</node5>
    </node3>
    <node7>ID</node7>
  </node2>
  <node2>
    <node3>
      <node4>Sky Star Panet Shoe</node4>
      <node5>Rusky Husky Musky Boo</node5>
    </node3>
    <node7>ID</node7>
  </node2>
</node1>';
$xml = new SimpleXMLElement($xmlStr); 
// seach records by tag value: 
// find nodes with  text
$res = $xml->xpath("node2[contains(., 'Musky')]"); 
echo "<pre>";
print_r($res); 

?>

它给出了正确的输出,我试过了

Array
(
    [0] => SimpleXMLElement Object
        (
            [node3] => SimpleXMLElement Object
                (
                    [node4] => Parrot Singer Truck Moo
                    [node5] => Tinker Musky Happy Fool
                )

            [node7] => ID
        )

    [1] => SimpleXMLElement Object
        (
            [node3] => SimpleXMLElement Object
                (
                    [node4] => Sky Star Panet Shoe
                    [node5] => Rusky Husky Musky Boo
                )

            [node7] => ID
        )

)

【讨论】:

  • 这适用于示例 xml。但是我使用的 xml 会返回很多错误,主要是:SimpleXMLElement::__construct(): Entity: line 12: parser error : xmlParseEntityRef: no name 我认为这是因为它有很多 " 、 ' 和 & 字符。我尝试使用 addslashes() 但随后出现此错误:SimpleXMLElement::__construct(): Entity: line 1: parser error : AttValue: " or ' expected
  • 那你应该告诉我,你应该解决这些错误,否则编写自定义代码将非常复杂,处理、管理......等等:)快乐的自定义编码。
  • 让我告诉你,我们有谷歌。你没有努力做简单的谷歌搜索吗? google.com/…
  • @SunilPachlandia 我已经改进了 XML 格式。虽然在 中有超过 50 个孩子,子孩子。我也处理了 & 问题。现在@jQuery.PHP.Magento.com 给出的解决方案应该在$res = $xml-&gt;xpath("node2[contains(., 'Musky')]"); 处修改?如果是,请帮助我如何在 DataNode 中搜索。更新后的问题中显示的 DataNode 的层次级别正是它在实际 XML 中的位置。 PS:我是新手,如果我问愚蠢的问题,我很抱歉。
【解决方案2】:

使用此代码,您可以找到您的搜索词。我已将其设为function,只需传递您的关键字,您就会得到结果,

function findWord($findVar)
{
    $catalog = simplexml_load_file("xmlfile.xml");

    $category = $catalog->node2;

    $found = 0;
    foreach($category as $c)
    {
        foreach($c->node3 as $node3)
        {
            $node4 = (string) ($node3->node4);
            $node5 = (string) ($node3->node5);
            if (stripos(strtolower($node4),strtolower($findVar)))
            {
                echo 'Found!!'.'<br/>';
                $found++;
            }
            if (stripos(strtolower($node5),strtolower($findVar)))
            {
                echo 'Found!!'.'<br/>';
                $found++;
            }
        }

        if (stripos(strtolower((string)$c->node7),strtolower($findVar)))
        {
            echo 'Found!!'.'<br/>';
            $found++;
        }   
    }
    if ($found == 0)
    {
        echo "No result";
    }
}

$findVar = 'Musky';
findWord($findVar);

【讨论】:

  • 函数只在node4、node5、node7中搜索?意思是如果node2里面有更多节点,它不会搜索它们?
  • 您必须为每个变量创建一个变量。
  • 好的。但是在那个 xml 中 node2 下可能有超过 50 个节点。所以这不会有好处。还是谢谢!
  • 等我找到更好的解决方案
  • @eNeMetcH,你没有检查我的解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多