使用 simpleXML 编辑 XML

发布于 2024-08-18 12:11:09 字数 70 浏览 2 评论 0原文

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

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

How can I edit the value's in a xml file using simpleXML ?

I know how to create the file, but not how to edit the value in an existing file ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岛歌少女 2024-08-25 12:11:09

当然,您可以使用 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();

看看 示例

Sure you can edit with 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();

Take a look at the examples.

慕巷 2024-08-25 12:11:09

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

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

Load your XML with SimpleXML and make the changes. Then you can use the asXML method to save the XML to a file (you pass the filename as the argument):

$xml = new SimpleXMLElement( $xmlString );
// do the manipulation here
$xml->asXML ( '/path/to/your/file.xml' );
陌伤ぢ 2024-08-25 12:11:09

请记住,虽然您可以使用 SimpleXML 编辑 XML,但也有一些限制。例如,您可以移除或删除节点或元素。您可以将其清除,使其变为空白,但无法完全消除它。为此,您需要 DOM 或类似的东西。

Keep in mind that although you can edit XML with SimpleXML, there are limitations. For example, you can remove or delete a node or element. You can clear it so that its blank, but you can't eliminate it altogether. For that, you need DOM, or something like that.

許願樹丅啲祈禱 2024-08-25 12:11:09

我正在这样工作(它完全相同,但它可能有帮助):
文件 test.xml 可以是任何扩展名,只要它是纯 xml 文本即可。

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<sitedata>
    <Texts>
        <ANode SomeAttr="Green" OtherAttr="Small"/>This is the text I'm changing.</ANode>
    </Texts>
</sitedata>

和 PHP 代码:

$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$SomeVar="<b>Text. This supports html code.</b><br/>I also work with variables, like GET or POST.";
$xml->Texts[0]->{'ANode'}=$SomeVar;
$xml->asXml('test.xml');

结果 test.xml:

<?xml version="1.0" encoding="utf-8"?>
<sitedata>
    <Texts>
    <ANode SomeAttr="Green" OtherAttr="Small"/><b>Text. This supports html code.</b><br/>I also work with variables, like GET or POST.</ANode>
    </Texts>
</sitedata>

希望有帮助!

I am working like this (it's quite the same but it could help):
The file test.xml could be any extension as long as it's a plain xml text.

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<sitedata>
    <Texts>
        <ANode SomeAttr="Green" OtherAttr="Small"/>This is the text I'm changing.</ANode>
    </Texts>
</sitedata>

And the PHP code:

$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$SomeVar="<b>Text. This supports html code.</b><br/>I also work with variables, like GET or POST.";
$xml->Texts[0]->{'ANode'}=$SomeVar;
$xml->asXml('test.xml');

Results test.xml:

<?xml version="1.0" encoding="utf-8"?>
<sitedata>
    <Texts>
    <ANode SomeAttr="Green" OtherAttr="Small"/><b>Text. This supports html code.</b><br/>I also work with variables, like GET or POST.</ANode>
    </Texts>
</sitedata>

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文