循环访问PHP xml_parser并转换为数组以便于参考

时间:2016-01-05 05:56:42

标签: php xml

我有以下XML $ result

    <Response>
      [.....]
       <Questions>
            <Question type="1" text="Which one of their favorite color?">
                <Answer correct="false">RED</Answer>
                <Answer correct="false">BLUE</Answer>
                <Answer correct="true">BLACK</Answer>
                <Answer correct="false">YELLOW</Answer>
            </Question>
            <Question type="2" text="What is their favorite food?">
                <Answer correct="false">PIZZA</Answer>
                <Answer correct="false">TACOS</Answer>
                <Answer correct="true">CAKE</Answer>
                <Answer correct="false">CHEESE</Answer>
            </Question>
            <Question type="3" text="Which person do they hate the most?">
                <Answer correct="false">Bill</Answer>
                <Answer correct="true">Jack</Answer>
                <Answer correct="false">Jammie</Answer>
                <Answer correct="false">Rick</Answer>
            </Question>                
        </Questions>
     </Response>

我知道我想用......

$xml = simplexml_load_string($result);

这会将$ xml设置为SimpleXML对象的结果。我想知道循环浏览这些问题文本的最佳方法,以便我可以提取所有问题并将它们添加到数组中,然后通过类型#,问题文本和答案轻松引用它们以便我可以做到

$QuestionText = $array["1"]["text"] //Which one is their favorite color?
$QuestionAnswer = $array["1"]["answer"] //BLACK

编辑#1

虽然我还没有得到回复,但我一直试图弄明白自己,并且我已经将我的方法更改为xml_parser ......

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($xml_parser, $result, $vals,$index);
xml_parser_free($xml_parser);

   $newResArr = array();
        foreach($vals as $val)
        {
            if(($val['tag']=='Question'))   {   
                if(isset($val['attributes'])){
                    $type = $val['attributes']['type'];
                    $newResArr[$type]['text'] = $val['attributes']['text'];
                      //I assume a foreach needs to be put here?
                    }
                }
        }

现在我可以通过$ newResArr [&#39; 1&#39;] [&#39; text&#39;]来引用文本,我得到了我想要的东西,但是我不知道怎么做如上所述设置foreach循环以在其属性设置为true时拉出答案。

1 个答案:

答案 0 :(得分:1)

我不是说这是最好的答案,但我认为这可能会对您有所帮助。请注意我添加了<text>标记。

<?php 
$result ='<Response>
       <Questions>
            <Question type="1" text="Which one of their favorite color?">
                <Answer type="1" correct="false"><text>RED</text></Answer>
                <Answer type="1" correct="false"><text>BLUE</text></Answer>
                <Answer type="1" correct="true"><text>BLACK</text></Answer>
                <Answer type="1" correct="false"><text>YELLOW</text></Answer>
            </Question>
            <Question type="2" text="What is their favorite food?">
                <Answer correct="false"><text>PIZZA</text></Answer>
                <Answer correct="false"><text>TACOS</text></Answer>
                <Answer correct="true"><text>CAKE</text></Answer>
                <Answer correct="false"><text>CHEESE</text></Answer>
            </Question>
            <Question type="3" text="Which person do they hate the most?">
                <Answer correct="false"><text>Bill</text></Answer>
                <Answer correct="true"><text>Jack</text></Answer>
                <Answer correct="false"><text>Jammie</text></Answer>
                <Answer correct="false"><text>Rick</text></Answer>
            </Question>                
        </Questions>
     </Response>';
//Will convert Object data to array
function objectToArray($object){
     return json_decode(json_encode($object),true,512,0);
}

$xml = objectToArray(simplexml_load_string($result));

$array = $xml['Questions']['Question'][0];
$QuestionText = $array['@attributes']['text'];//Which one is their favorite color?
$QuestionAnswer = $array['Answer'];

foreach ($QuestionAnswer as $value) {
    if($value['@attributes']['correct'] == 'true'){
        echo 'The correct answer is '.$value['text'];
    } 
}
echo '<pre>';
print_r($array);
//You can also try to print the whole array
print_r($xml);
echo '</pre>';

我希望有所帮助。