0

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>
xenoterracide
  • 16,274
  • 24
  • 118
  • 243

1 Answers1

3

Just fetch the textual value of the DoItUsernamePwdResult element (e.g. 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;
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
Slaven Rezic
  • 4,571
  • 14
  • 12
  • as a note, I also figured out just as you were answering this that once I had the node I could use `->textContent` to get it back as XML – xenoterracide Sep 18 '13 at 19:39
  • I have the exact same problem, but feeding the extracted string back to the parser (which is working) has the problem that named entities in the encapsulated XML will be expanded after parsing and getting values from the DOM. So I always end up with a parser error on some documents. – jackthehipster Aug 26 '14 at 10:24