【问题标题】:CDATA in XML Arrays extractionXML 数组中的 CDATA 提取
【发布时间】:2023-03-21 16:00:01
【问题描述】:

您好,我有一段 XML 输出包含

<Question type="2" text="Which one of the following area codes is associated with you?">
<Answer correct="false">606</Answer>
<Answer correct="false">859</Answer>
<Answer correct="false">616</Answer>
<Answer correct="false">614/380</Answer>
<Answer correct="false">812</Answer>
<Answer correct="true">502</Answer>
<Answer correct="false">810</Answer>
<Answer correct="false">740</Answer>
<Answer correct="false">248</Answer>
<Answer correct="false">None of the above</Answer>
</Question>

我正在使用它来解析响应

$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);

返回结果时,我得到了问题的类型,但没有得到正确的值字段。是因为结构的深度吗?

【问题讨论】:

    标签: php json xml


    【解决方案1】:

    是的,似乎只是第一个(信封)在数组中获取其属性。

    你需要遍历它们。

    例子:

    $response = '<Question type="2" text="Which one of the following area codes is associated with you?">
        <Answer correct="false">606</Answer>
        <Answer correct="false">859</Answer>
        <Answer correct="false">616</Answer>
        <Answer correct="false">614/380</Answer>
        <Answer correct="false">812</Answer>
        <Answer correct="true">502</Answer>
        <Answer correct="false">810</Answer>
        <Answer correct="false">740</Answer>
        <Answer correct="false">248</Answer>
        <Answer correct="false">None of the above</Answer>
        </Question>';
    
    $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;
    }
    
    $arrOutput = json_decode(json_encode($objXmlDocument), true);
    unset($arrOutput['Answer']);
    foreach ($objXmlDocument as $key => $answer) {
        $arrOutput[$key][] = json_decode(json_encode($answer), true);
    }
    
    echo var_export($arrOutput, true) . PHP_EOL;
    

    输出:

    [
        '@attributes' => [
            'type' => '2',
            'text' => 'Which one of the following area codes is associated with you?',
        ],
        'Answer'      => [
            0 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '606',
            ],
            1 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '859',
            ],
            2 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '616',
            ],
            3 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '614/380',
            ],
            4 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '812',
            ],
            5 => [
                '@attributes' => [
                    'correct' => 'true',
                ],
                0             => '502',
            ],
            6 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '810',
            ],
            7 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '740',
            ],
            8 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => '248',
            ],
            9 => [
                '@attributes' => [
                    'correct' => 'false',
                ],
                0             => 'None of the above',
            ],
        ],
    ];
    

    您现在只需从该数据中整理出您想要的内容。

    编辑:这不是“优雅”。也许您应该使用属性 get 方法等来构建您需要的数组。

    【讨论】:

      【解决方案2】:

      好吧,避开 json_decode 路线。太多的工作。这是一种优雅的方式 - 干净整洁的代码,人们很容易理解。

      <?php
          $xmlstring = '<Question type="2" text="Which one of the following area codes is associated with you?">
      <Answer correct="false">606</Answer>
      <Answer correct="false">859</Answer>
      <Answer correct="false">616</Answer>
      <Answer correct="false">614/380</Answer>
      <Answer correct="false">812</Answer>
      <Answer correct="true">502</Answer>
      <Answer correct="false">810</Answer>
      <Answer correct="false">740</Answer>
      <Answer correct="false">248</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>";
      }
      

      输出如下:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-10
        • 2020-03-11
        • 2010-11-01
        • 1970-01-01
        • 2020-09-21
        相关资源
        最近更新 更多