如何解析php中的xml? [重复]

时间:2023-01-15 08:12:22

This question already has an answer here:

这个问题在这里已有答案:

I'm using Codeigniter 3 and I tried to take value of xml. I didn't handle..

我正在使用Codeigniter 3,我试图获取xml的值。我没处理..

I tried to take value with this code.

我尝试使用此代码获取值。

        $dom = new DOMDocument();
        $dom->loadlXML($xml);
        $img = $dom->getElementsByTagName('sonucKodu')->item(0);
        echo $img->attributes->getNamedItem("value")->value;

My xml file.

我的xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<SYSMessage xmlns:ext="http://nxxx/Cd/Extensions/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <messageType code="1" value="xxx" codeSystemGuid="0a9ba485-e7e0-4abb-9c86-0a14fd364bb8" version="1" />
   <documentGenerationTime value="201512041717" />
   <author>
      <healthcareProvider code="0" value="SYS" codeSystemGuid="c9dbe1cb-57cb-48fb-bdd3-d622e0e304c6" version="1" />
   </author>
   <recordData>
      <KayitCevabi>
         <sonucKodu value="E2003" />
         <sonucMesaji value="TakipNo geçerli değil" />
      </KayitCevabi>
   </recordData>
</SYSMessage>

I just need value of attribute sonucKodu. It must be E2003.

我只需要属性sonucKodu的值。它必须是E2003。

Thanks for your helping.

谢谢你的帮助。

2 个解决方案

#1


1  

I'd use SimpleXMLElement rather than Domdocument.

我使用SimpleXMLElement而不是Domdocument。

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SYSMessage xmlns:ext="http://nxxx/Cd/Extensions/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <messageType code="1" value="xxx" codeSystemGuid="0a9ba485-e7e0-4abb-9c86-0a14fd364bb8" version="1" />
   <documentGenerationTime value="201512041717" />
   <author>
      <healthcareProvider code="0" value="SYS" codeSystemGuid="c9dbe1cb-57cb-48fb-bdd3-d622e0e304c6" version="1" />
   </author>
   <recordData>
      <KayitCevabi>
         <sonucKodu value="E2003" />
         <sonucMesaji value="TakipNo geçerli değil" />
      </KayitCevabi>
   </recordData>
</SYSMessage>';
$s_xml = new SimpleXMLElement($xml);
//print_r($s_xml);
echo $s_xml->recordData->KayitCevabi->sonucKodu['value'];

Output:

输出:

E2003

E2003

Demo: https://eval.in/480525

演示:https://eval.in/480525

#2


0  

<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
?>

#1


1  

I'd use SimpleXMLElement rather than Domdocument.

我使用SimpleXMLElement而不是Domdocument。

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SYSMessage xmlns:ext="http://nxxx/Cd/Extensions/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <messageType code="1" value="xxx" codeSystemGuid="0a9ba485-e7e0-4abb-9c86-0a14fd364bb8" version="1" />
   <documentGenerationTime value="201512041717" />
   <author>
      <healthcareProvider code="0" value="SYS" codeSystemGuid="c9dbe1cb-57cb-48fb-bdd3-d622e0e304c6" version="1" />
   </author>
   <recordData>
      <KayitCevabi>
         <sonucKodu value="E2003" />
         <sonucMesaji value="TakipNo geçerli değil" />
      </KayitCevabi>
   </recordData>
</SYSMessage>';
$s_xml = new SimpleXMLElement($xml);
//print_r($s_xml);
echo $s_xml->recordData->KayitCevabi->sonucKodu['value'];

Output:

输出:

E2003

E2003

Demo: https://eval.in/480525

演示:https://eval.in/480525

#2


0  

<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
?>