简体   繁体   中英

How can I parse escaped XML with XML::LibXML

So the responses are basically full XML documents escaped as a string inside of another XML Document. What is the best way to extract this String and Parse it into something I can simply get the values from.

(will accept simply how to get it back to raw XML)

  <soap:Body>
    <DoItUsernamePwdResponse xmlns="http://www.aspdotnetstorefront.com/">
      <DoItUsernamePwdResult>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;AspDotNetStorefrontImportResult Version="9.2" DateTime="9/18/2013 1:46:06 PM"&gt;&lt;Item NodeType="Entity" Name="Nike" GUID="84775261-731D-4E11-BB82-FA5F61BC61C5" ID="1" ActionTaken="Add" Status="OK" Message="" /&gt;&lt;/AspDotNetStorefrontImportResult&gt;</DoItUsernamePwdResult>
    </DoItUsernamePwdResponse>
  </soap:Body>

Just fetch the textual value of the DoItUsernamePwdResult element (eg using findnodes ) and just feed the result again into the XML::LibXML parser. Something like this could work:

use strict;
use XML::LibXML;

my $xmlxml = <<'EOF';
  <soap:Body xmlns:soap="something">
    <DoItUsernamePwdResponse xmlns="http://www.aspdotnetstorefront.com/">
      <DoItUsernamePwdResult>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;AspDotNetStorefrontImportResult Version="9.2" DateTime="9/18/2013 1:46:06 PM"&gt;&lt;Item NodeType="Entity" Name="Nike" GUID="84775261-731D-4E11-BB82-FA5F61BC61C5" ID="1" ActionTaken="Add" Status="OK" Message="" /&gt;&lt;/AspDotNetStorefrontImportResult&gt;</DoItUsernamePwdResult>
    </DoItUsernamePwdResponse>
  </soap:Body>
EOF

my $xml = XML::LibXML->new->parse_string($xmlxml)->findvalue('//*[local-name()="DoItUsernamePwdResult"]');
warn XML::LibXML->new->parse_string($xml)->serialize;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM