【问题标题】:Inserting XML data to JSON array in Index.php在 Index.php 中将 XML 数据插入 JSON 数组
【发布时间】:2019-11-26 02:56:28
【问题描述】:

对不起,如果这是一个愚蠢的问题,我只是感觉迷失在这里。我正在尝试将某些值从我的 XML 文件中获取到我的普通 PHP 文件中的 JSON 数组中。

这就是我收集 XML 文件数据的方式以及我的 JSON 的显示方式,具体取决于哪种情况为真:

switch ($creditId) {
case 123:
    $xml = new XMLReader;
    $xml->open('123.xml');

    $doc = new DOMDocument;


    while ($xml->read() && $xml->name !== 'Decision');


    while ($xml->name === 'Decision')
    {


        $node = simplexml_import_dom($doc->importNode($xml->expand(), 
true));

        $reason_text = $node->Reason->ReasonText ;
        echo "$reason_text \n";

        // go to next <DecisionResponse>
        $xml->next('Decision');
    }


        echo '[{
            "creditId": 123,
            "client": "Peter",
            "Decision": "Solo" ,
            "Factors": ["REASONTEXT1","REASONTEXT2"]
    }]';

    break;
case 789:

 $xml = new XMLReader;
    $xml->open('789.xml');

    $doc = new DOMDocument;


    while ($xml->read() && $xml->name !== 'Decision');


      while ($xml->name === 'Decision')
       {


        $node = simplexml_import_dom($doc->importNode($xml->expand(), 
true));

        $reason_text = $node->Reason->ReasonText ;
        echo "$reason_text \n";

        // go to next <DecisionResponse>
        $xml->next('Decision');
       }


    echo '[{
        "creditId": 789,
        "client": "Jonas",
        "Decision": "Random",
        "Factors": ["REASONTEXT1","REASONTEXT2"]
    }]';
    break;
default:
    http_response_code(404);
}

REASONTEXT1 和 REASONTEXT2 是变量 $reason_text 的值应显示在 JSON 数组中的位置。例如:我的 123 案例的 json 显示现在看起来像这样($reason_text 的回声输出值 RandomReason1 和 RandomReason2):

 RandomReason1 
 RandomReason2
    [{
            "creditId": 123,
            "client": "Peter",
            "Decision": "Solo",
            "Factors": ["REASONTEXT1","REASONTEXT2"]
    }]

我希望这些值显示如下:

    [{
            "creditId": 123,
            "client": "Peter",
            "Decision": "Solo",
            "Factors": ["RandomReason1","RandomReason2"]
    }]

最后,我想将某些 XML 数据插入 JSON 数组。提前谢谢!

【问题讨论】:

    标签: javascript json vue.js


    【解决方案1】:

    请注意,这是一个 php 问题,与 vue 或 js 无关。现在你的代码。它在 json 之前打印 RandomReason1 和 RandomReason2 因为你的代码上有这个echo "$reason_text \n";,你应该在你的 json 中打印它。 因此,将值存储在一个数组中,然后在需要时将其 ECHO。 例如:

    $reasons[] = $reason_text;
    ...
    echo '[{
        "creditId": 789,
        "client": "Jonas",
        "Decision": "Random",
        "Factors": '.json_encode($ereasons).'
    }]';
    

    【讨论】:

      【解决方案2】:

      没有你的 XML 就很难了,但是我们开始吧 :-)

      假设您的 XML 如下所示:

      <?xml version="1.0" encoding="UTF-8"?>
      <root>
        <Decision>
          <Reason>
            <ReasonText>This is my text #1</ReasonText>
          </Reason>
        </Decision>
        <Decision>
          <Reason>
            <ReasonText>This is my text #2</ReasonText>
          </Reason>
        </Decision>
      </root>
      

      我为案例 123 制作了一个工作示例:

              case 123:
                  $xml = new XMLReader;
                  $xml->open('123.xml');
      
                  $doc = new DOMDocument;
      
      
                  while ($xml->read()){
                      $nodename=$xml->localName;
                      $depth=$xml->depth;
                      $gipchecked=0;
                      if ($nodename=='Decision' &&  $xml->nodeType == XMLReader::ELEMENT){ //EN van het type start elemenet
                              $node = simplexml_import_dom($doc->importNode($xml->expand(),true));
                              $reason_text.= $node->Reason->ReasonText ;
                              echo "Reason text contains: $reason_text \r\n";
                      }
      
                      // go to next <DecisionResponse>
                      // $xml->next('Decision');
                  }
                  $xml->close();
      
      
                      echo '[{
                          "creditId": 123,
                          "client": "Peter",
                          "Decision": "Solo" ,
                          "Factors": '.json_encode($reason_text).'
                  }]';
      
              break;
      

      如果你运行它,它将在你的浏览器中显示以下结果:

      Reason text contains: This is my text #1 
      
      Reason text contains: This is my text #1This is my text #2 
      
      [{ "creditId": 123, "client": "Peter", "Decision": "Solo" , "Factors": "This is my text #1This is my text #2" }]
      

      请务必在最后关闭您的 xml,如下所示:

       $xml->close();
      

      除此之外,我还会仔细查看您的案例陈述。实际上,您需要更改文件名(如果是 789,则变为 789.xml)和客户端 + 决定。您不需要为每种情况复制 XMLREADER 的一部分。

      如果您将该部分动态地放在您的 switch 语句下方,您可以为所有情况执行一次。 DRY or Don't Repeat Yourself 是软件工程中的一个编程概念,它试图减少代码中的冗余。 如果您的脚本中有多次相同的代码并且您即将进行调整,那么您将忘记在其他地方执行此操作。尽量避免这种情况!

      【讨论】:

        猜你喜欢
        • 2014-04-26
        • 1970-01-01
        • 2020-03-15
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 2018-05-19
        相关资源
        最近更新 更多