繁体   English   中英

将 XML 数据插入到 Index.php 中的 JSON 数组

[英]Inserting XML data to JSON array in Index.php

对不起,如果这是一个愚蠢的问题,我只是在这里感到迷茫。 我试图从我的 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 数组中。 先感谢您!

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

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

没有你的 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 或 Don't Repeat Yourself 是软件工程中的一个编程概念,它试图减少代码中的冗余。 如果您的脚本中有多次相同的代码,并且您要进行调整,那么更改就是您将忘记在其他地方进行调整。 为了避免这种情况!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM