Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Beginning PHP 5.3
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP 5.3
This is the forum to discuss the Wrox book Beginning PHP 5.3 by Matt Doyle; ISBN: 978-0-470-41396-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 5.3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 12th, 2012, 05:51 PM
Authorized User
Points: 128, Level: 2
Points: 128, Level: 2 Points: 128, Level: 2 Points: 128, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Feb 2011
Posts: 25
Thanks: 2
Thanked 1 Time in 1 Post
Default Adding an elements to existing xml document

I've gone over the code a million times, and I've even downloaded the code from this website and still can't seem to get it to work. Can anyone help me. The code is:
PHP Code:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <title>Adding an Element to an XML File with the DOM Extension</title>
    <link rel="stylesheet" type="text/css" href="common.css">
    <h1>Adding an Element to an XML File with the DOM Extension</h1>


// Load XML file
$doc = new DOMDocument();
$doc->preserveWhiteSpace false;
$doc->load"./stock_list.xml" );
$doc->formatOutput true;

// Get the stocklist root element
$stockListElements $doc->getElementsByTagName(  "stockList" );
$stockList $stockListElements->item);

// Create a new "item" element and add it to the stocklist
$item $doc->createElement"item" );
$item->setAttribute"type""vegetable" );
$stockList->appendChild$item );

// Create the item's "name" child element
$name $doc->createElement"name""carrot" );
$item->appendChild$name );

// Create the item's "unitprice " child element
$unitPrice $doc->createElement"unitPrice""0.79" );
$item->appendChild$unitPrice );

//Create item's quantity element
$quantity $doc->createElement"quantity""31" );
$item->appendChild$quantity );

// Create item's description element
$description $doc->createElement"description" );
$item->appendChild$description );
$cdata $doc->createCDATASection"Carrots are crunchy" );
$description->appendChild$cdata );

//Output the XML document, encoding markup characters as needed
echo htmlspecialchars$doc->saveXML() );

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a node to an existing xml node list. codehelp C# 2008 aka C# 3.0 2 October 12th, 2009 07:41 AM
Adding a new Node to existing XML document Lupus81 XML 3 August 4th, 2009 04:57 PM
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
Comparing elements in an XML document dlorenz XSLT 0 August 7th, 2007 05:28 PM
Adding elements from an XML to XSL. AjayLuthria XSLT 1 May 1st, 2007 12:12 PM

All times are GMT -4. The time now is 07:12 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.