Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.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
Javascript xml解析中未定义的对象属性处理_Javascript_Php_Xml_Json_Codeigniter - Fatal编程技术网

Javascript xml解析中未定义的对象属性处理

Javascript xml解析中未定义的对象属性处理,javascript,php,xml,json,codeigniter,Javascript,Php,Xml,Json,Codeigniter,已定义属性的XML对象解析成功,但未定义属性的XML对象解析不成功。假设我们没有属性,并尝试将其分配给变量,抛出一个错误,如下所示: 代码如下所示: $data['id']=$xml[0]->response->list->region[0]->id 但如果不存在id属性,则会遇到上述错误 在下列条件下进行试验: 如果$xml[0]->响应->列表->区域[0]->id 如果$xml[0]->response->list->region[0]->id==false 如果$xml[0]->resp

已定义属性的XML对象解析成功,但未定义属性的XML对象解析不成功。假设我们没有属性,并尝试将其分配给变量,抛出一个错误,如下所示:

代码如下所示:

$data['id']=$xml[0]->response->list->region[0]->id

但如果不存在id属性,则会遇到上述错误

在下列条件下进行试验:

如果$xml[0]->响应->列表->区域[0]->id

如果$xml[0]->response->list->region[0]->id==false

如果$xml[0]->response->list->region[0]->id==未定义

如果!是\u对象$xml[0]->响应->列表->区域[0]->id

所有这些都是不成功的

解析代码为:

$url           =  'xmloutputtingurl';
$xmlContents   =   $this->curl->simple_get($url);   
$xml       =   implexml_load_string($xmlContents);
$data['xml']   =   xml;
接收的xml数据是:

其xml作为数组的输出为:

object(SimpleXMLElement)[19]
  public 'request' => 
    object(SimpleXMLElement)[20]
      public 'state' => string 'California' (length=10)
      public 'city' => string 'Los Angeles' (length=11)
      public 'childtype' => string 'neighborhood' (length=12)
  public 'message' => 
    object(SimpleXMLElement)[21]
      public 'text' => string 'Request successfully processed' (length=30)
      public 'code' => string '0' (length=1)
  public 'response' => 
    object(SimpleXMLElement)[22]
      public 'region' => 
        object(SimpleXMLElement)[23]
          public 'id' => string '12447' (length=5)
          public 'latitude' => string '34.020921' (length=9)
          public 'longitude' => string '-118.411732' (length=11)
      public 'subregiontype' => string 'neighborhood' (length=12)
      public 'list' => 
        object(SimpleXMLElement)[24]
          public 'count' => string '96' (length=2)
          public 'region' => 
            array (size=95)
在某些情况下,如果在列表->区域标记下未提及id,则会导致错误

有人能帮上忙吗?还有,在解析json时,如何通过JavaScript实现同样的场景


提前感谢

在PHP中,您可以通过使用DOM和Xpath而不是SimpleXml来避免它。这是一个不同的概念,您使用表达式来获取零件

$dom = new DOMDocument();
$dom->loadXml('<foo attr="bar"/>');
$xpath = new DOMXpath($dom);

var_dump(
  //the attribute value of foo/@attr
  $xpath->evaluate('string(/foo/@attr)'),
  //asking for the value of an non existing attribute returns an empty string
  $xpath->evaluate('string(/foo/@nonexisting)')
);
var_dump(
  //asking for an element will return a node list
  $xpath->evaluate('/foo')->length,
  // if no element is found an empty list is returned
  $xpath->evaluate('/bar')->length
);
Xpath表达式可以包含条件,让我们获取没有id子级的区域元素:

/*/响应/列表/区域[notid]

或者使用空id

/*/响应/列表/区域[notstringid=]

DOMXpath::evaluate的第二个参数是当前上下文节点。所以,您可以获取节点列表,迭代它们,并使用相对于它们的表达式

$dom = new DOMDocument();
$dom->loadXml('<foo attr="bar"><bar/></foo>');
$xpath = new DOMXpath($dom);

foreach ($xpath->evaluate('//*') as $node) {
  var_dump(
    $node->nodeName,
    // count first parent node - 0 or 1
    $xpath->evaluate('count(ancestor::*[1])', $node),
    // count parent nodes without the attribute
    $xpath->evaluate('count(ancestor::*[not(@attr)])', $node)
  );
}
在JavaScript中,大多数人已经在不知不觉中完成了。你曾经在jQuery中使用过CSS选择器吗?您不仅限于浏览器DOM,加载或生成的XML也可以使用浏览器DOM。CSS选择器不如Xpath表达式强大,但JS中的API抵消了这一点。

什么是$xml[0]?向我们展示生成$xml的解析代码。
$dom = new DOMDocument();
$dom->loadXml('<foo attr="bar"><bar/></foo>');
$xpath = new DOMXpath($dom);

foreach ($xpath->evaluate('//*') as $node) {
  var_dump(
    $node->nodeName,
    // count first parent node - 0 or 1
    $xpath->evaluate('count(ancestor::*[1])', $node),
    // count parent nodes without the attribute
    $xpath->evaluate('count(ancestor::*[not(@attr)])', $node)
  );
}