duanfei9278 2015-04-14 06:21
浏览 27

在XML中进行字符串搜索后返回父节点 - PHP

I have an XML file like so:

<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>

I would like to know how I would inject a search string "Musky" to a PHP function and get back <DataNode>...</DataNode> & <DataNode>...</DataNode> back?

Essentially I would like to search a huge XML file for a string and return all the DataNode's which contain the string back.

If this is possible with SimpleXML it would be great. Else any other solution is also fine.

EDIT: Notice how "Musky" can be in different nodes under <DataNode>

  • 写回答

2条回答 默认 最新

  • douchun1859 2015-04-14 06:30
    关注

    use

    $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); 
    

    //For testing purpost just copy paste following code in editor , For testing , I didnt use separate xml file.

    <?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); 
    
    ?>
    

    It gives proper output , i tried

    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
            )
    
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line