doumengjing1500
2014-12-19 18:34
浏览 225
已采纳

PHP - 使用simplexml_load_string解析XML - 使用CDATA获取空值? [重复]

This question already has an answer here:

When parsing an XML array like:

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product>
    <Code>ABC-1001</Code>
    <Brand>ZCOM</Brand>
  </Product>
</Products>

I get an output of:

Array
(
  [0] => Array
  (
    [Code] => AP1024-DDRII640
    [Brand] => ZCOM
  )
}

But when the XML is like:

<?xml version="1.0" encoding="utf-8"?>
<Products Code="ABC-1001">
  <Product>
    <Code><![CDATA[ABC-1001]]></Code>
    <Brand><![CDATA[ZCOM]]></Brand>
  </Product>
</Products>

It returns:

array
  0 => 
    array (size=12)
      '@attributes' => 
        array (size=1)
          'Code' => string 'ABC-1001' (length=8)
      'Code' => 
        array (size=0)
          empty
      'Brand' => 
        array (size=0)
          empty

This is how the XML is parsed from a URL:

$updateUrl = file_get_contents('http://www.someplace/xmlfeed/xml.cfm?asd=12345&uhg=9999');
$updateXml=<<<XML
$updateUrl
XML;
$updateXmlObject=json_decode(json_encode((array) simplexml_load_string($updateXml)), 1);
$updatePHPArray=$updateXmlObject['Product'];

And:

var_dump($updatePHPArray);exit;

Gives the output as above.

Now, why am I getting empty values in the second instance and how could I remedy this without access to the XML source?

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • PHP:如何处理&lt;![CDATA [使用SimpleXMLElement? 4个答案\ r

    解析XML数组时: \ n

     &lt;?xml version =“1.0”encoding =“utf-8”?&gt; 
    &lt; Products&gt; 
    &lt; Product&gt; 
    &lt; Code&gt; ABC-1001&lt; / 代码&gt; 
    &lt; Brand&gt; ZCOM&lt; / Brand&gt; 
    &lt; / Product&gt; 
    &lt; / Products&gt; 
       
     
     

    我得到的输出为:< / p>

     数组
    (
     [0] =&gt;数组
    (
     [代码] =&gt; AP1024-DDRII640 
     [品牌] =&gt; ZCOM \  n)
    } 
       
     
     

    但是当XML如下:

     &l  t;?xml version =“1.0”encoding =“utf-8”?&gt; 
    &lt; Products Code =“ABC-1001”&gt; 
    &lt; Product&gt; 
    &lt; Code&gt;&lt;![CDATA [  ABC-1001]]&gt;&lt; / Code&gt; 
    &lt; Brand&gt;&lt;![CDATA [ZCOM]]&gt;&lt; / Brand&gt; 
    &lt; / Product&gt; 
    &lt; / Products&gt; 
     <  / code>  
     
     

    它返回:

      array 
     0 =&gt;  
     array(size = 12)
    '@attributes'=&gt;  
     array(size = 1)
    'Code'=&gt; 字符串'ABC-1001'(长度= 8)
    '代码'=&gt;  
     array(size = 0)
     empty 
    'Brand'=&gt;  
     array(size = 0)
     empty 
       
     
     

    这是从URL解析XML的方式:

       $ updateUrl = file_get_contents('http://www.someplace/xmlfeed/xml.cfm?asd = 12345&amp; uhg = 9999'); 
     $ updateXml =&lt;&lt;&lt; XML 
     $ updateUrl \  nXML; 
     $ updateXmlObject = json_decode(json_encode((array)simplexml_load_string($ updateXml)),1); 
     $ updatePHPArray = $ updateXmlObject ['Product']; 
       
     
      n 

    并且:

      var_dump($ updatePHPArray); exit; 
       
     
     

    如上所示给出输出 。

    现在,为什么我在第二个实例中获取空值,如何在不访问XML源的情况下解决此问题?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duannan4486 2014-12-19 18:47
    已采纳

    The problem seems to be that the cast you're doing to array can return results different than the actual structure of the XML object.

    Something like the following code should give you an array with the correct info:

    $array = array_map('strval', (array) $xml->Product);
    

    Take care you cast those parts to string of which you'll get the data from (in the example done via strval()). In the opposite, json_encode() is not working well with SimpleXMLElement.

    点赞 评论

相关推荐 更多相似问题