doumao1887 2015-08-21 10:55
浏览 20

Php检索XML元素并在服务器上编辑文件

I've searched and tried a number of examples and answers I've found here, but I think my requirements are quite specific and I haven't been able to find an answer that's worked for me so far... Bodging a number of answers together hasn't worked either!

Aim - Update the hosplist_divert value of the same element that contains a specific name tag.

The XML file is hosted in a sperate folder to the php page, in those case at /data/hospitals2.xml

My XML is like so:

<Document>
  <Placemark>
    <name>UHSM Wythenshawe</name>
    <hosplist_divert>1</hosplist_divert>
  </Placemark>
</Document>

There are approx 50 Placemark entries in the file.

So far I am only able to return all the hospital names from the file with,

// Create the SXE object
// You can read from file using the simplexml_load_file function
$url = "http://www.patientpathfinder.co.uk/user/nwasdos/data/hospitals2.xml";
$sxe = new SimpleXMLElement($url, NULL, TRUE);
$sxe->registerXPathNamespace('hospital','http://earth.google.com/kml/2.2');

// Fetch the right HOSPITAL using XPATH
// hospital name stored in Document/Placemark/name
// dovert status stored in Document/Placemark/hosplist_divert

//trying to find above values for UHSM Wythenshawe

$result=$sxe->xpath('//hospital:name[.="UHSM Wythenshawe"]/parent::*');
  foreach ($result as $hospital)
 {
   echo $hospital . "<br>";
 }

// Update the values you want
//$target_hosp[0]->hosplist_divert = 'ON DIVERT';


// Store the updated values in the $xml variable
//$xml = $sxe->asXML();

// Print the updated XML
//echo $xml;

}

It took me about half a day to realise that I needed to define the namespace, but haven't really had time to understand why the namespace is required and would be happy to remove it from the XML file in. Favour of a working solution.

Thanks to all that contribute, Nick

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。
    • ¥15 livecharts wpf piechart 属性
    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了