简体   繁体   中英

Trouble Extracting CDATA from Multi Level XML Result (API)

Hi i have an issue extracting DATA from XML Output. The XML is as follows...

<Question type="5" text="What state was your SSN issued in?">
<Answer correct="false">Maryland</Answer>
<Answer correct="false">Alaska</Answer>
<Answer correct="false">Ohio</Answer>
<Answer correct="false">Indiana</Answer>
<Answer correct="false">Missouri</Answer>
<Answer correct="false">Washington</Answer>
<Answer correct="false">Arkansas</Answer>
<Answer correct="false">Illinois</Answer>
<Answer correct="true">Kentucky</Answer>
<Answer correct="false">None of the above</Answer>
</Question>

My challenge is that when i use this code

$ch = curl_init($serviceUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// Debug output of the response
libxml_use_internal_errors(TRUE);
 
$objXmlDocument = simplexml_load_string($response,null,LIBXML_NOCDATA);
 
if ($objXmlDocument === FALSE) {
    echo "There were errors parsing the XML file.\n";
    foreach(libxml_get_errors() as $error) {
        echo $error->message;
    }
    exit;
}
 
$objJsonDocument = json_encode($objXmlDocument);
$arrOutput = json_decode($objJsonDocument,true);

I can pull the data as an array and it all works fine with one exception. The correct attribute falls off the resulting array. and can not be called.

It appears that array will take the question attributes type and text but since it is a sublevel of that it doesn't pickup on the correct attribute.

My XML/Json knowledge is alright but this one has me stumped. Any ideas would be great.

this is how i am parsing the data for the other parts of the project

$question_1 = $arrOutput['Response']['Questions']['Question']['0']['@attributes']['text'];
$answer_choices_one = $arrOutput['Response']['Questions']['Question'][0]['Answer'];

My hope is when i use a foreach loop to build out the question choices to pickup on the correct attribute and store it in the value field of the input field i am using... I just can't seem to get the darn value.

Thanks for your insights.

the below code not only extracts the List of possible answers, but it also displays the question, its type and the possible answers returned from the API.

    <?php

$xmlstring = '<Question type="5" text="What state was your SSN issued in?">
<Answer correct="false">Maryland</Answer>
<Answer correct="false">Alaska</Answer>
<Answer correct="false">Ohio</Answer>
<Answer correct="false">Indiana</Answer>
<Answer correct="false">Missouri</Answer>
<Answer correct="false">Washington</Answer>
<Answer correct="false">Arkansas</Answer>
<Answer correct="false">Illinois</Answer>
<Answer correct="true">Kentucky</Answer>
<Answer correct="false">None of the above</Answer>
</Question>';

$initialize = new SimpleXMLElement($xmlstring);

//extract the attributes text and type below
$accessQuestionText = $initialize->attributes()->text;
$accessQuestionType = $initialize->attributes()->type;

//using for each loop lets iterate over the Possible Answers
foreach($initialize->Answer as $answers){

    echo "Question of Type $accessQuestionType is $accessQuestionText ";
    echo "The List of Dropdown answers can be ".$answers."<br>";
}

The Output is as follows; 在此处输入图片说明

Hope this is of great help to you.

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