0
votes

I got simplexml_load_string(): Entity: line 2: parser error : Specification mandate value for attribute Auth when try to convert xml to array using php simplexml_load_string

here is my XML

<?xml version="1.0" encoding="utf-8"?>
<AssociateUserToPMSResponse Auth/UserToken="somestring" warning="Already existed in database">
<Success>true</Success>
</AssociateUserToPMSResponse>

when I try to use tidy class Auth/UserToken removed from XML. how to get the value of Auth/UserToken?

1

1 Answers

0
votes

As the XML isn't playing well as XML, it's possible to use DOM to load it as badly formed HTML. The following code will load the content - one slight side effect of the load is that the tag names are all converted to lower case (I use 'success' rather than 'Success' in the XPath expression).

<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );

$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<AssociateUserToPMSResponse Auth/UserToken="somestring" warning="Already existed in database">
<Success>true</Success>
</AssociateUserToPMSResponse>
XML;

$doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
$doc->loadHTML($xml);
$xp = new DOMXPath($doc);
$s = $xp->query("//success");
echo $doc->saveHTML($s[0]);

Also if you output the content of the document, you find that the first entity is loaded as...

<associateusertopmsresponse auth warning="Already existed in database">

So rather than loading it with the /, it's loaded as though it's two attributes, the first without a value.