Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于PHP SimpleXML和xpath的一些初学者问题_Php_Xpath_Simplexml - Fatal编程技术网

关于PHP SimpleXML和xpath的一些初学者问题

关于PHP SimpleXML和xpath的一些初学者问题,php,xpath,simplexml,Php,Xpath,Simplexml,我正在学习PHP SimpleXML,我有一些问题。 我一直在玩游戏,从我工作的内联网上获取代码。只要可能,我就需要通用代码,因为代码可以随时更改。 在我的示例中,我选择了一个div标记及其所有子项 上面的代码可以工作,但只有在我们编写正确的标记名的情况下。(->ul,-->li,-->a.) 在不必每次指定子项名称的情况下循环浏览其所有内容的通用方法是什么?(->ul,-->li,-->a.) 此外,我更希望不必将其转换为数组,除非方法正确。 我一直在尝试children()属性,但它不起作用

我正在学习PHP SimpleXML,我有一些问题。 我一直在玩游戏,从我工作的内联网上获取代码。只要可能,我就需要通用代码,因为代码可以随时更改。 在我的示例中,我选择了一个div标记及其所有子项

上面的代码可以工作,但只有在我们编写正确的标记名的情况下。(->ul,-->li,-->a.) 在不必每次指定子项名称的情况下循环浏览其所有内容的通用方法是什么?(->ul,-->li,-->a.) 此外,我更希望不必将其转换为数组,除非方法正确。
我一直在尝试children()属性,但它不起作用,它停止并在该行崩溃:foreach($result->children()as$ul)

提前非常感谢您花时间阅读我的问题。欢迎任何帮助:)

系统信息:
symfony 1.4.17,带有sfWebBrowserPlugin、cURL dadapter。
PHP5.4.0,支持卷曲,卷曲信息7.24.0

  • 我不知道我自己从来没用过

  • 我不知道我通常使用Zend Debug-但我还是不明白你的问题。。。我想你漏掉了几个字:-)

  • 2.1可能是xdebug/eclipse。Id检查首选项可能有一个限制递归量的设置,以帮助管理内存

    2.2
    SimpleXML::xpath
    始终返回匹配节点的数组。这就是为什么要使用整数索引数组作为结果。因此,如果您执行
    //someelement
    操作,您将得到一个包含所有
    someelement
    标记的数组。然后,您可以以正常方式访问它们的子代,如
    $someelement->itschildelement


    2.3
    $result->children()
    是从一般意义上理解事物的好方法。如果Xdebug正在崩溃,那就是Xdebug。要么关闭它,要么忽略它,要么找一个不同的调试器:-)Xdebug是一个工具,但不应该指定如何实现它。

    我想现在我完全理解了问题2.2和2.3

    正如您所解释的,因为它的xpath返回的是数组[1],而不是SimpleXML对象,所以我永远不能使用$result->children(),因为php数组没有children()属性hehe。(我有点傻哈哈)

    正如您所解释的,解决方案很简单,计算数组的元素数,循环到元素中,然后使用children属性(如果是SimpleXML对象)再次循环。我将在下面添加正确的代码

    我还将向他们的论坛提交Eclipse Watch或xdebug的第1点问题,以猜测真正的问题是什么


    谢谢你,神童,非常有用的回答:)

    工作起来很有魅力呵呵

    在这里,我添加了一个完整的函数,该函数递归地搜索节点和子节点的所有区域中的子字符串,并返回找到它的完整字符串

    在我的例子中,搜索诸如href=之类的值和其他动态生成的标记值是完美的。 还显示了我们上面讨论的内容的实现。也许它可以改进,并且可以添加更多的安全检查。

    如何称呼它:


    你能用curl完成这个任务吗。解析数据很容易谢谢你的建议,我还将用php cURL测试它,看看它是否比这个方法简单。我读到cURL是从Web获取代码的速度更快的适配器之一。@DarkWish:不是。sfWebBrowser只是一个CURL包装器,内置了将文本响应转换为DOMDocument、SimpleXML等的方法。在sfWebBrowser的文档中,说明了web客户端可以使用3个适配器,CURL是其中速度最快的一个。这是真的。。。这也是默认设置,因此,除非您处于某个被上帝抛弃的环境中,而cURL不可用,或者除非您特意使用另一个适配器,否则将使用curlRewrite point 2以更易于理解,谢谢您的指出。(这本身不是一个问题)。重写第2.3点,我想说的是所有的碰撞和无法接触到儿童。(执行和调试都会崩溃)。慢慢阅读您的答案ty。您还需要确保检查元素
    是否有子元素
    。此外,我相信您从get调用的
    children
    元素在子元素集合中包含其属性,因此您需要计划过滤掉这些属性。ty、函数和属性在这里有很好的解释:
    ...
      <div class="cabTabs">
          <ul>
              <li><a href="/link1">Info1</a></li>
              <li><a href="/link2">Info2</a></li>
              <li><a href="/link3">Info3</a></li>
          </ul>
      </div>
    ...
    
    
    //Get all web content:
    $b = new sfWebBrowser(); //using symfony 1.4.17 sfWebBrower to get a SimpleXML object.
    $b->get('http://intranetwebexample'); //returns a sfWebBrower object.
    $xml = $b->getResponseXML(); //returns a SimpleXMLElement
    
    //[Eclipse xdebug Watch - $xml]
    "$xml"    SimpleXMLElement     
      @attributes Array [3]   
      head    SimpleXMLElement    
      body    SimpleXMLElement
    
    
    //Get the div class="cabTabs".
    $result = $xml->xpath('//descendant::div[@class="cabTabs"]'); 
    
    //[Eclipse xdebug Watch - $result]
    "$result" Array [1]   
      0   SimpleXMLElement    
          @attributes Array [1]   
              class   cabTabs 
          ul  SimpleXMLElement    
              li  Array [6]
    
    foreach ($result as $record) {        
        foreach($record->ul as $ul) { 
            foreach($ul->li as $li) {
                foreach($li->a as $a) {
                    echo ' ' . $a->name;
                }
            }
        }
    }
    
    /* public function bSimpleXMLfindfullstringwithsubstring($node, $sSearchforsubstring, &$sFullstringfound, &$bfoundsubstring)
     * Recursive function to search for the first substring in a list of SimpleXML objects, looking in all its children, in all their attributes.
     * Returns true if the substring has been found.
     * Parameter return:
     *   $sFullstringfound: returns the full string where the substring has been found.
     *   $bfoundsubstring: returns true if the substring has been found.
    */
    
    public function bSimpleXMLfindfullstringwithsubstring($node, $sSearchforsubstring, &$sFullstringfound, &$bfoundsubstring=false)
    {
      $bRet = false; 
      if ((isset($node) && ($bfoundsubstring == false)))
      {
          //If the node has attributes
          if ($node->attributes()->count() > 0)
          {
              //Search the string in all the elements of the current SimpleXML object.
              foreach ($node->attributes() AS $name => $attribute)  //[$name = class , (string)$attribute = cabTabs, $attribute = SimpleXML object]
              {
                  //(Take care of charset if necessary).
                  if (stripos((string)$attribute, $sSearchforsubstring) !== false)
                  {
                      //substring found in one of the attributes.
                      $sFullstringfound = (string)$attribute;
                      $bfoundsubstring = true;
                      $bRet = true;
                      break;
                  }
              }
          }
    
          //If the node has childrens (subnodes)
          if (($node->count() > 0) && ($bfoundsubstring == false))
          {
              foreach ($node->children() as $nodechildren)
              {
                  if ($bfoundsubstring == false)
                  {
                      //Search in the next children.
                      self::bSimpleXMLfindfullstringwithsubstring($nodechildren, $sSearchforsubstring, $sFullstringfound, $bfoundsubstring);
                  }
                  else
                  {
                      break;
                  }
              }
          }
      }
      return $bRet;
    }
    
    $b = new sfWebBrowser();
    $b->get('http://www.example.com/example.html');
    $xml = $b->getResponseXMLfixed();     
    $result = $xml->xpath('//descendant::div[@class="cabTabs"]'); //example
    
    $sFullString = "";
    $bfoundsubstring = false;
    foreach ($result as $record)
    {
      self::bSimpleXMLfindfullstringwithsubstring($record, "/substring/tosearch", $sFullString, $bfoundsubstring);
    }