【问题标题】:Undefined object property handling in xml parsingxml解析中未定义的对象属性处理
【发布时间】:2014-02-28 16:00:36
【问题描述】:

已定义属性的 XML 对象解析成功,但未定义属性未成功。假设我们没有属性并尝试将其分配给变量,会引发如下错误:

下面的代码是这样写的:

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

但是如果没有id属性存在,则遇到上述错误

通过以下条件尝试:

  1. if($xml[0]->response->list->region[0]->id)

  2. if($xml[0]->response->list->region[0]->id==false)

  3. if($xml[0]->response->list->region[0]->id=="undefined")

  4. if (!is_object($xml[0]->response->list->region[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)

如果在某些情况下 list->region 标签下没有提到 id,则会导致错误

任何人都可以帮助解决这个问题,以及在解析 json 时如何从 JavaScript 实现相同的场景。

提前致谢

【问题讨论】:

  • 什么是$xml[0]?向我们展示生成 $xml 的解析代码。

标签: javascript php xml json codeigniter


【解决方案1】:

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

输出:

string(3) "bar"
string(0) ""
int(1)
int(0)

Xpath 表达式可以包含条件,让我们获取没有 id 子项的区域元素:

/*/response/list/region[not(id)]

或使用空 id

/*/response/list/region[not(string(id) = "")]

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 也是如此。 CSS 选择器不如 Xpath 表达式强大,但这被 JS 中的 API 所抵消。

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多