php 如何使用PHP修改xml文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1193528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:27:42  来源:igfitidea点击:

How to modify xml file using PHP

phpxml

提问by Avinash

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

我想根据以下标准在 PHP 中修改我的 xml 文件。

my xml structure look like this:

我的 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>

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

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

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

意味着,在第一次搜索字符串==“酒店”时,与酒店相关的数据将按上述格式存储在xml文件中。

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

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

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.

并且搜索例如学校的不同查询在与学校相关的数据被附加到xml文件时完成。与酒店的数据。

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

现在再次搜索学校,然后它将删除与学校相关的元素。

Thanks in advance.

提前致谢。

回答by Cristian Toma

You can use the DOMDocumentfrom PHP.

您可以使用DOM文档PHP

You load your file and than loop trough the childNodesof the document.

您加载文件,然后循环遍历文档的childNode

<?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();
?>

You can save your output XML like this

您可以像这样保存输出 XML

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

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

要在JavaScript 中进行解析,您应该使用jQuery(一个小而强大的库)。

You can include the library directly from Google Code Repository.

您可以直接从 Google 代码库中包含该库。

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

The library is cross-browser and very small. It should be cached in many cases, because some sites use it from 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();
});

回答by adatapost

Document Object Model (DOM) is the answer.

文档对象模型 (DOM) 就是答案。

<?
 $dom=new DomDocument();
 $dom->Load("file.xml");
 $root=$dom->documentElement;
 ...
 $dom->Save("file.xml");
?>