使用simpleXML编辑XML

Oli*_*ton 11 php xml simplexml

如何使用simpleXML编辑xml文件中的值?

我知道如何创建文件,但不知道如何编辑现有文件中的值?

cle*_*tus 14

当然可以使用SimpleXML进行编辑:

$input = <<<END
<?xml version='1.0' standalone='yes'?>
<documents>
  <document>
    <name>spec.doc</name>
  </document>
</documents>
END;

$xml = new SimpleXMLElement($input);
$xml->document[0]->name = 'spec.pdf';
$output = $xml->asXML();
Run Code Online (Sandbox Code Playgroud)

看看这些例子.


Jan*_*čič 7

使用SimpleXML加载XML并进行更改.然后,您可以使用asXML方法将XML保存到文件中(将文件名作为参数传递):

$xml = new SimpleXMLElement( $xmlString );
// do the manipulation here
$xml->asXML ( '/path/to/your/file.xml' );
Run Code Online (Sandbox Code Playgroud)