如何使用PHP修改xml文件 [英] How to modify xml file using PHP

查看:100
本文介绍了如何使用PHP修改xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要根据以下条件在PHP中修改我的xml文件.

I want to modify my xml file in PHP based on the following criteria.

我的xml结构如下:

<?xml version="1.0" encoding="ISO-8859-1" ?> 

<markers>
   <marker>
      <type></type>
      <title></title>
      <address></address>
      <latitude></latitude>
      <longitude></longitude>
   <marker>
   <marker>
      <type></type>
      <title></title>
      <address></address>
      <latitude></latitude>
      <longitude></longitude>
   <marker>
</markers>

现在每次要修改xml时,都带有type属性.

Now every time when xml going to modify, with the type attribute.

意味着,在第一次搜索字符串=="hotels"时,与旅馆有关的数据将以上述格式存储在xml文件中.

means, on first time search string == "hotels" then data related to hotels will be stored in xml file as above format.

现在,当再次搜索酒店时,它将删除具有带有值酒店的子元素的元素.

Now when again search for hotels is done then it will remove the elements which have child element with value hotels.

并搜索其他查询,例如那时完成了学校的工作,与学校有关的数据被附加到xml文件中.加上酒店的数据.

and search for different query for e.g. schools is done at that time data related to the schools are appended to the xml file. with the data of the hotels.

现在再次搜索学校已完成,然后将删除与学校有关的元素.

now again search for schools is done then it will remove the element related to schools.

谢谢.

推荐答案

您可以使用 PHP 中的 DOMDocument .

您加载文件,然后遍历文档的 childNodes .

You load your file and than loop trough the childNodes of the document.

<?php
$dom=new DOMDocument();
$dom->load("file.xml");

$root=$dom->documentElement; // This can differ (I am not sure, it can be only documentElement or documentElement->firstChild or only firstChild)

$nodesToDelete=array();

$markers=$root->getElementsByTagName('marker');

// Loop trough childNodes
foreach ($markers as $marker) {
    $type=$marker->getElementsByTagName('type')->item(0)->textContent;
    $title=$marker->getElementsByTagName('title')->item(0)->textContent;
    $address=$marker->getElementsByTagName('address')->item(0)->textContent;
    $latitude=$marker->getElementsByTagName('latitude')->item(0)->textContent;
    $longitude=$marker->getElementsByTagName('longitude')->item(0)->textContent;

    // Your filters here

    // To remove the marker you just add it to a list of nodes to delete
    $nodesToDelete[]=$marker;
}

// You delete the nodes
foreach ($nodesToDelete as $node) $node->parentNode->removeChild($node);

echo $dom->saveXML();
?>

您可以这样保存输出XML

You can save your output XML like this

$dom->saveXML(); // This will return the XML as a string
$dom->save('file.xml'); // This saves the XML to a file

要在 JavaScript 中进行此解析,您应该使用 jQuery (一个小型但功能强大的库).

To do this parsing in JavaScript you should use jQuery (a small, but powerful library).

您可以直接从Google代码存储库中添加该库.

You can include the library directly from Google Code Repository.

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

该库是跨浏览器的,非常小.在许多情况下,应该对其进行缓存,因为某些网站从Google Code中使用它

$(yourXMLStringOrDocument).find("marker").each(function () {
     var marker=$(this);

     var type=marker.find('type').text();
     var title=marker.find('title').text();
     var address=marker.find('address').text();
     var latitude=marker.find('latitude').text();
     var longitude=marker.find('longitude').text();
});

这篇关于如何使用PHP修改xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆