Hi,
I am trying to add to an rss file using the dom. Here is my code:
<?php
#load an XML document into the DOM
$dom = new DomDocument();
$dom -> load("PHPDocument13.xml");
#create elements
$channel = $dom->createElement("channel");
$item = $dom -> createElement("item");
$title = $dom -> createElement("title");
$link = $dom -> createElement("link");
$description = $dom -> createElement("description");
#create text nodes
$titleText = $dom -> createTextNode("Php Developer");
$linkText = $dom -> createTextNode("www.fliwox.com");
$descriptionText = $dom -> createTextNode("PHP developers needed for fast paced development environment.");
#append the text nodes to the inner nested elements
$title->appendChild($titleText);
$link->appendChild($linkText);
$description->appendChild($descriptionText);
#append the inner nested elements to the <title> element
$item -> appendChild($title);
$item -> appendChild($link);
$item -> appendChild($description);
//append the <item> element to the <channel> element
$channel->appendChild($item);
#append the <channel> element to the root element <rss>
$dom->documentElement->appendChild($channel);
#create a new enlarged xml document
$dom -> save("new.xml");
?>
This is the output of my code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/rss</link>
<description>New RSS tutorial on W3Schools</description>
</item>
<item>
<title>XML Tutorial</title>
<link>http://www.w3schools.com/xml</link>
<description>New XML tutorial on W3Schools</description>
</item>
</channel>
<channel><item><title>Php Developer</title><link>www.fliwox.com</link><description>PHP developers needed for fast paced development environment.</description></item></channel>
</rss>
Do you notice how my script appended another channel element at the end as opposed to appending the <item> to the <channel> element? How can I change my script so that it just appends the <item> within the <channel> tag?
Thank you 🆒