douzhenzu0247 2014-07-25 03:08
浏览 42
已采纳

将嵌套标记值推送到PHP数组中

How can you push the sample_id value into the array using PHP DOM, along with first and samples?

I've tried adding another foreach loop, but I'm getting various notices such as

Trying to get property of non-object

XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<maintable>
  <group>
    <first>text1</first>
    <second>text2</second>
    <samples a="link" b="link">
      <sample_id>000555</sample_id>
    </samples>
  </group>
  <group>
    <first>text3</first>
    <second>text4</second>
    <samples a="link" b="link">
      <sample_id>111000</sample_id>
    </samples>
  </group>  
</maintable> 

PHP

<?php
$text = file_get_contents($file);
$content = iconv('UTF-8', 'UTF-8//IGNORE', $text);

$xml = new DOMDocument('1.0', 'ISO-8859-1');
$xml->loadXML($content);
$xpath = new DOMXPath( $xml );


$query = '/maintable/group[first="text3"]';                 
$nodes = $xpath->query( $query );


$var = array();
foreach( $nodes as $k => $node )
{
    foreach( $node->childNodes as $cnode )
    {
        if ($cnode->nodeName == 'first')
        {
                $var[$k]["first"] = (string)$cnode->textContent;
        }

        if ($cnode->nodeName == 'samples')
        {
                $var[$k]["samp_url"] = (string)$cnode->getAttribute("a");

        }

        if ($cnode->nextSibling->nodeName == 'sample_id')
        {
                $var[$k]["sample_id"] = (string)$cnode->nextSibling->textContent;

        }
    }   
}
//print_r($cnode->childNodes);
?>

Attempt to loop thru nodeList

$var = array();

$nodes = $xpath->evaluate('/maintable/group[first="text3"]');
foreach ($nodes as $node) 
{
   foreach( $node->childNodes as $cnode )
   {
       if (is_array($cnode))
       {
           foreach( $cnode->childNodes as $cn )
           {
               $var[]["sample_id"] = (string)$cn->textContent;
           }
       }
   }   
}

var_dump($var);
  • 写回答

2条回答 默认 最新

  • doudoulb1234 2014-07-25 09:34
    关注

    DOMXpath::query() can only return node lists, DOMXpath::evaluate() can return scalars, too. The second argument of query()/evaluate() is the context node. If provided, the Xpath expression will be relative to it.

    So you can select the node(s) first, iterate the list and use evaluate() again to fetch the values:

    $xml = new DOMDocument();
    $xml->loadXML($content);
    $xpath = new DOMXPath($xml);
    
    $results = [];
    
    $nodes = $xpath->evaluate('/maintable/group[first="text3"]');
    foreach ($nodes as $node) {
      $results[] = [
        'first' => $xpath->evaluate('string(first)', $node),
        'sample_url' => $xpath->evaluate('string(samples/@a)', $node),
        'sample_id' => $xpath->evaluate('string(samples/sample_id)', $node)
      ];
    }
    
    var_dump($results);
    

    Output: https://eval.in/171656

    array(1) {
      [0]=>
      array(3) {
        ["first"]=>
        string(5) "text3"
        ["sample_url"]=>
        string(4) "link"
        ["sample_id"]=>
        string(6) "111000"
      }
    }
    

    Fetching only the sample id from the document works, too:

    var_dump(
      $xpath->evaluate('string(/maintable/group[first="text3"]/samples/sample_id)')
    );
    

    Output:

    string(6) "111000"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP