I need to create a new XML file and write that to my server. So, I am looking for the best way to create a new XML file, write some base nodes to it, save it. Then open it again and write more data.
我需要创建一个新的XML文件并将其写入服务器。因此,我正在寻找创建一个新的XML文件、为它编写一些基本节点、保存它的最佳方法。然后再次打开它并编写更多的数据。
I have been using file_put_contents()
to save the file. But, to create a new one and write some base nodes I am not sure of the best method.
我一直在使用file_put_contents()保存文件。但是,要创建一个新的节点并编写一些基节点,我不确定最好的方法是什么。
Ideas?
想法吗?
3 个解决方案
#1
59
DOMDocument is a great choice. It's a module specifically designed for creating and manipulating XML documents. You can create a document from scratch, or open existing documents (or strings) and navigate and modify their structures.
DOMDocument是一个不错的选择。它是专门为创建和操作XML文档而设计的模块。您可以从头创建一个文档,或者打开现有文档(或字符串),导航和修改它们的结构。
$xml = new DOMDocument();
$xml_album = $xml->createElement("Album");
$xml_track = $xml->createElement("Track");
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
$xml->save("/tmp/test.xml");
To re-open and write:
重新写:
$xml = new DOMDocument();
$xml->load('/tmp/test.xml');
$nodes = $xml->getElementsByTagName('Album') ;
if ($nodes->length > 0) {
//insert some stuff using appendChild()
}
//re-save
$xml->save("/tmp/test.xml");
#2
13
PHP has several libraries for XML Manipulation.
PHP有几个用于XML操作的库。
The Document Object Model (DOM) approach (which is a W3C standard and should be familiar if you've used it in other environments such as a Web Browser or Java, etc). Allows you to create documents as follows
文档对象模型(DOM)方法(是W3C标准,如果您在其他环境(如Web浏览器或Java等)中使用过它,那么应该很熟悉它)。允许您创建如下文档
<?php
$doc = new DOMDocument( );
$ele = $doc->createElement( 'Root' );
$ele->nodeValue = 'Hello XML World';
$doc->appendChild( $ele );
$doc->save('MyXmlFile.xml');
?>
Even if you haven't come across the DOM before, it's worth investing some time in it as the model is used in many languages/environments.
即使您以前没有遇到过DOM,也值得花一些时间在它上,因为模型在许多语言/环境中使用。
#3
2
With FluidXML you can generate and store an XML document very easily.
使用FluidXML,您可以很容易地生成和存储XML文档。
$doc = fluidxml();
$doc->add('Album', true)
->add('Track', 'Track Title');
$doc->save('album.xml');
Loading a document from a file is equally simple.
从文件中加载文档同样简单。
$doc = fluidify('album.xml');
$doc->query('//Track')
->attr('id', 123);
https://github.com/servo-php/fluidxml
https://github.com/servo-php/fluidxml
#1
59
DOMDocument is a great choice. It's a module specifically designed for creating and manipulating XML documents. You can create a document from scratch, or open existing documents (or strings) and navigate and modify their structures.
DOMDocument是一个不错的选择。它是专门为创建和操作XML文档而设计的模块。您可以从头创建一个文档,或者打开现有文档(或字符串),导航和修改它们的结构。
$xml = new DOMDocument();
$xml_album = $xml->createElement("Album");
$xml_track = $xml->createElement("Track");
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
$xml->save("/tmp/test.xml");
To re-open and write:
重新写:
$xml = new DOMDocument();
$xml->load('/tmp/test.xml');
$nodes = $xml->getElementsByTagName('Album') ;
if ($nodes->length > 0) {
//insert some stuff using appendChild()
}
//re-save
$xml->save("/tmp/test.xml");
#2
13
PHP has several libraries for XML Manipulation.
PHP有几个用于XML操作的库。
The Document Object Model (DOM) approach (which is a W3C standard and should be familiar if you've used it in other environments such as a Web Browser or Java, etc). Allows you to create documents as follows
文档对象模型(DOM)方法(是W3C标准,如果您在其他环境(如Web浏览器或Java等)中使用过它,那么应该很熟悉它)。允许您创建如下文档
<?php
$doc = new DOMDocument( );
$ele = $doc->createElement( 'Root' );
$ele->nodeValue = 'Hello XML World';
$doc->appendChild( $ele );
$doc->save('MyXmlFile.xml');
?>
Even if you haven't come across the DOM before, it's worth investing some time in it as the model is used in many languages/environments.
即使您以前没有遇到过DOM,也值得花一些时间在它上,因为模型在许多语言/环境中使用。
#3
2
With FluidXML you can generate and store an XML document very easily.
使用FluidXML,您可以很容易地生成和存储XML文档。
$doc = fluidxml();
$doc->add('Album', true)
->add('Track', 'Track Title');
$doc->save('album.xml');
Loading a document from a file is equally simple.
从文件中加载文档同样简单。
$doc = fluidify('album.xml');
$doc->query('//Track')
->attr('id', 123);
https://github.com/servo-php/fluidxml
https://github.com/servo-php/fluidxml