Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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/8/perl/9.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/1/php/270.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
如何从XML::Simple创建的结构中提取CDATA值?_Xml_Perl_Cdata - Fatal编程技术网

如何从XML::Simple创建的结构中提取CDATA值?

如何从XML::Simple创建的结构中提取CDATA值?,xml,perl,cdata,Xml,Perl,Cdata,我在模块中使用Perl脚本来解析XML提要: my $xml=XMLin($resp->content); my $xml2 = $xml->{params}{param}{value}{array}{data}{value}{struct}{member}; print $xml2->{prod_id}{value}{int}; #works fine print $xml2->{product_text}{value}{string}; #returns a h

我在模块中使用Perl脚本来解析XML提要:

my $xml=XMLin($resp->content);
my $xml2 = $xml->{params}{param}{value}{array}{data}{value}{struct}{member};


print $xml2->{prod_id}{value}{int}; #works fine

print $xml2->{product_text}{value}{string}; #returns a hash reference (hash(0x...)
要获得文本,我必须修改哪些内容:

<div class="text"> <div class="text_products">sdfsdf</div> </div>
sdfsdf
而不是散列引用

这是XML数据:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <methodResponse>
- <params>
- <param>
- <value>
- <array>
- <data>
- <value>
- <struct>
- <member>
  <name>product_text</name> 
- <value>
  <string><![CDATA[ <div class="text"> <div class="text_products">sdfsdf</div> </div> ]]></string> 
  </value>
  </member>
- <member>
  <name>prod_id</name> 
- <value>
  <int>1290</int> 
  </value>
  </member>
- <member>.......

- 
- 
- 
- 
- 
- 
- 
- 
- 
产品文本
- 
sdfsdf]]>
- 
产品标识
- 
1290
- .......

您的代码似乎适合我。我复制了您的XML和perl代码,它打印:

1290 <div class="text"> <div class="text_products">sdfsdf</div> </div> 
1290sdfsdf
使用Data::Dumper将显示存在的数据结构。例如,当我运行您的示例代码时,相关部分的摘录如下:

'value' => {
  'string' => ' <div class="text"> <div class="text_products">sdfsdf</div> </div> '
}
“值”=>{
'字符串'=>'sdfsdf'
}
我认为你可能会得到一些不同的东西的唯一原因是,如果在同一级别有另一个元素叫做product_text

编辑:
如果有时得到的是空元素,并且它们作为hashref出现,则可以在调用
XMLin
时将
SuppressEmpty
选项设置为undef。有关更多详细信息,请参阅文档(链接:)

哈希引用的内容是什么?如果使用Data::Dumper,您应该看到hash.print转储程序中有哪些键可用($xml2->{product_text}{value}{string});生成以下输出:$VAR1=“sdfsdf]]>”;我使用xml::Simple2.18版本,我使用$xml=XMLin($resp->content);打印转储程序($xml);显示所有xml数据。如何使用数据转储程序显示键值对?已编辑以进一步解释有关data::dumper的信息。请担心我的错误。。xml发件人站点出错。谢谢你,伙计,你完全正确。xml服务器出现配置错误。元素“product_text”已空提交到带有xml::simple的脚本。我在数据转储程序中使用的另一个脚本收到了数据,因为调用是用另一个过程(xml::rpc)完成的,很高兴能提供帮助。我对答案进行了编辑,为您提供了有关XML::Simple选项的提示,这些选项可以让您更优雅地处理这两种情况。