-1

我有这样的输出格式api:

Array
(
   [return] => 
           <CEK_STATUS>
             <HEADER>
                <NO_BARANG>ID1901422741</NO_BARANG> 
                <TGL_HOUSE_BLAWB>2017/11/30</TGL_HOUSE_BLAWB> 
                <KD_RESPON>408</KD_RESPON><WK_REKAM>2017/12/03 14:47:28</WK_REKAM>
            </HEADER>
          </CEK_STATUS>
)

谁能帮我解析每个 HEADER->NO_BARAN ?谢谢

4

1 回答 1

0

例如,您可以将SimpleXMLElementDOMDocument与 xpath 表达式一起使用,并使用 aforeach来获取每个HEADER->NO_BARANG.

$data = <<<DATA
<CEK_STATUS>
    <HEADER>
        <NO_BARANG>ID1901422741</NO_BARANG>
        <TGL_HOUSE_BLAWB>2017/11/30</TGL_HOUSE_BLAWB>
        <KD_RESPON>408</KD_RESPON><WK_REKAM>2017/12/03 14:47:28</WK_REKAM>
    </HEADER>
</CEK_STATUS>
DATA;

$array = [
    "return" => $data
];

$obj = simplexml_load_string($array["return"]);

foreach($obj as $d) {
   echo $d->NO_BARANG . "<br>";
}

foreach ($obj->xpath('/CEK_STATUS/HEADER') as $item) {
    echo $item->NO_BARANG . "<br>";
}

$doc = new DOMDocument();
$doc->loadXML($data);
$xpath = new DOMXpath($doc);

foreach ($xpath->query('/CEK_STATUS/HEADER/NO_BARANG') as $i) {
     echo $i->nodeValue . "<br>";
}

演示

于 2018-03-13T07:50:07.533 回答