XML DOM PHP 添加子节点到 root/links/link

发布于 2024-12-02 00:28:21 字数 1367 浏览 0 评论 0原文

我有一个小问题...这是我的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<links>

    <link>
        <id>432423</id>
        <href>http://www.google.ro</href>
    </link>

    <link>
        <id>5432345</id>
        <href>http://www.youtube.com</href>
    </link>

    <link>
        <id>5443</id>
        <href>http://www.yoursite.com</href>
    </link>

</links>

我怎样才能添加另一个

    <link>
        <id>5443</id>
        <href>http://www.yoursite.com</href>
    </link>

我只设法向 ROOT/LINKS 添加一条记录 ->使用 xpath 链接,这里是代码

<?php

$doc = new DOMDocument();
$doc->load( 'links.xml' );


$links= $doc->getElementsByTagName("links");

$xpath = new DOMXPath($doc);
$hrefs = $xpath->evaluate("/links");

$href = $hrefs->item(0);
$item = $doc->createElement("item");

    /*HERE IS THE ISSUE...*/
    $link = $doc->createElement("id","298312800");
    $href->appendChild($link);
    $link = $doc->createElement("link","www.anysite.com");
    $href->appendChild($link);

$href->appendChild($item);

print $doc->save('links.xml');

echo "the link has been added!";

?>

任何帮助将不胜感激:D

I have a little question ... this is my xml:

<?xml version="1.0" encoding="UTF-8"?>
<links>

    <link>
        <id>432423</id>
        <href>http://www.google.ro</href>
    </link>

    <link>
        <id>5432345</id>
        <href>http://www.youtube.com</href>
    </link>

    <link>
        <id>5443</id>
        <href>http://www.yoursite.com</href>
    </link>

</links>

How can i ad another

    <link>
        <id>5443</id>
        <href>http://www.yoursite.com</href>
    </link>

??

I managed only to add a record to ROOT/LINKS -> LINK using xpath, and here is the code

<?php

$doc = new DOMDocument();
$doc->load( 'links.xml' );


$links= $doc->getElementsByTagName("links");

$xpath = new DOMXPath($doc);
$hrefs = $xpath->evaluate("/links");

$href = $hrefs->item(0);
$item = $doc->createElement("item");

    /*HERE IS THE ISSUE...*/
    $link = $doc->createElement("id","298312800");
    $href->appendChild($link);
    $link = $doc->createElement("link","www.anysite.com");
    $href->appendChild($link);

$href->appendChild($item);

print $doc->save('links.xml');

echo "the link has been added!";

?>

Any help would be appreciated :D

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

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

发布评论

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

评论(2

兰花执着 2024-12-09 00:28:21
$doc = new DOMDocument();

// Setting formatOutput to true will turn on xml formating so it looks nicely
// however if you load an already made xml you need to strip blank nodes if you want this to work
$doc->load('links.xml', LIBXML_NOBLANKS);
$doc->formatOutput = true;

// Get the root element "links"
$root = $doc->documentElement;

// Create new link element
$link = $doc->createElement("link");

// Create and add id to new link element
$id = $doc->createElement("id","298312800");
$link->appendChild($id);

// Create and add href to new link element
$href = $doc->createElement("href","www.anysite.com");
$link->appendChild($href);

// Append new link to root element
$root->appendChild($link);

print $doc->save('links.xml');

echo "the link has been added!";
$doc = new DOMDocument();

// Setting formatOutput to true will turn on xml formating so it looks nicely
// however if you load an already made xml you need to strip blank nodes if you want this to work
$doc->load('links.xml', LIBXML_NOBLANKS);
$doc->formatOutput = true;

// Get the root element "links"
$root = $doc->documentElement;

// Create new link element
$link = $doc->createElement("link");

// Create and add id to new link element
$id = $doc->createElement("id","298312800");
$link->appendChild($id);

// Create and add href to new link element
$href = $doc->createElement("href","www.anysite.com");
$link->appendChild($href);

// Append new link to root element
$root->appendChild($link);

print $doc->save('links.xml');

echo "the link has been added!";
碍人泪离人颜 2024-12-09 00:28:21

XPath 用于定位 XML 文档中的节点,而不是操作树。尝试$dom->appendChild($new_link)

XPath is used to locate nodes in an XML document, not to manipulate the tree. Try $dom->appendChild($new_link).

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