Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Beginning PHP 5.3
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 12th, 2012, 05:51 PM
Authorized User
 
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:
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Adding an Element to an XML File with the DOM Extension</title>
    <link rel="stylesheet" type="text/css" href="common.css">
  </head>
  <body>
    <h1>Adding an Element to an XML File with the DOM Extension</h1>
    <pre>

<?php

// 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() );

?>
      </pre>
    </body>
</html>
Thanks





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





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