通过 PHP 在 XML 文件中添加新节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15201433/
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 08:46:06  来源:igfitidea点击:

adding a new node in XML file via PHP

phpxmlxml-parsingsimplexml

提问by user2083529

I just wanted to ask a question .. how can i insert a new node in an xml using php. my XML file (questions.xml) is given below

我只是想问一个问题..如何使用php在xml中插入一个新节点。我的 XML 文件 (questions.xml) 在下面给出

<?xml version="1.0" encoding="UTF-8"?>
<Quiz>
   <topic text="Preparation for Exam">
      <subtopic text="Science" />
      <subtopic text="Maths" />
      <subtopic text="english" />
   </topic>
</Quiz>

I want to add a new "subtopic" with "text" attribute, that is "geography". How can i do this using PHP? Thanks in advance though. well my code is

我想添加一个带有“文本”属性的新“副主题”,即“地理”。我如何使用 PHP 做到这一点?不过先谢谢了。我的代码是

<?php

$xmldoc = new DOMDocument();
$xmldoc->load('questions.xml');



$root = $xmldoc->firstChild;

$newElement = $xmldoc->createElement('subtopic');
$root->appendChild($newElement);

// $newText = $xmldoc->createTextNode('geology'); // $newElement->appendChild($newText);

// $newText = $xmldoc->createTextNode('geology'); // $newElement->appendChild($newText);

$xmldoc->save('questions.xml');

?>

?>

回答by Butt4cak3

I'd use SimpleXMLfor this. It would look somehow like this:

我会为此使用SimpleXML。它看起来像这样:

// Open and parse the XML file
$xml = simplexml_load_file("questions.xml");
// Create a child in the first topic node
$child = $xml->topic[0]->addChild("subtopic");
// Add the text attribute
$child->addAttribute("text", "geography");

You can either display the new XML code with echo or store it in a file.

您可以使用 echo 显示新的 XML 代码,也可以将其存储在文件中。

// Display the new XML code
echo $xml->asXML();
// Store new XML code in questions.xml
$xml->asXML("questions.xml");

回答by MatRt

The best and safe way is to load your XML document into a PHP DOMDocument object, and then go to your desired node, add a child, and finally save the new version of the XML into a file.

最好且安全的方法是将您的 XML 文档加载到一个 PHP DOMDocument 对象中,然后转到您想要的节点,添加一个子节点,最后将新版本的 XML 保存到一个文件中。

Take a look at the documentation : DOMDocument

看一下文档:DOMDocument

Example of code:

代码示例:

// open and load a XML file
$dom = new DomDocument();
$dom->load('your_file.xml');

// Apply some modification
$specificNode = $dom->getElementsByTagName('node_to_catch');
$newSubTopic = $xmldoc->createElement('subtopic');
$newSubTopicText = $xmldoc->createTextNode('geography');
$newSubTopic->appendChild($newSubTopicText);
$specificNode->appendChild($newSubTopic);

// Save the new version of the file
$dom->save('your_file_v2.xml');

回答by Nirmal Ram

You can use PHP's Simple XML.You have to read the file content, add the node with Simple XML and write the content back.

您可以使用 PHP 的Simple XML。您必须读取文件内容,使用 Simple XML 添加节点并将内容写回。