BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
PHP XML
PHP DOMNode Function lookupPrefix
PHP DOMNode Function lookupNamespaceURI
PHP DOMNode Function isSupported
PHP DOMNode Function isSameNode
PHP DOMNode Function insertBefore
PHP DOMNode Function hasChildNodes
PHP DOMNode Function hasAttributes
PHP DOMNode Function cloneNode
PHP DOMNodelist Function item
PHP DOMNamedNodeMap Function item
PHP DOMNamedNodeMap Function getNamedItemNS
PHP DOMNamedNodeMap Function getNamedItem
PHP DOMImplementation Function hasFeature
PHP DOMImplementation Function createDocumentType
PHP DOMImplementation Function createDocument
PHP DOMElement Function removeAttributeNS
PHP DOMElement Function removeAttributeNode
PHP DOMElement Function removeAttribute
PHP DOMElement Function hasAttributeNS
PHP DOMElement Function getAttribute
 
 

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :365

 

DOMNode Function replaceChild

PHP Version: (No version information might be only in CVS. I have used this function in PHP5)

Description

This function replaces a child by another node.

Usage:

objectDOMNode->replaceChild ( object newnode, object oldnode)

This function replaces the child oldnode with the passed new node. If the new node is already a child it will not be added a second time. If the replacement succeeds the old node is returned.

Throws DOMException if node cannot be replaced.

PHP Code:

<?php

$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<employee>
<employeename id="myId">test1</employeename>
<employeeaddress>testAddress1</employeeaddress>
<SSN>11111</SSN>
<company>XYZ Corporation</company>
</employee>
XML;

$dom = new domDocument;

//load the XML string into the DOM

$dom->loadXML($xmlstr);

//retrieve the first element with name employee

$employee = $dom->getElementsByTagName('employee')->item(0);

//retrieve the first element with name employeeaddress

$employeeaddress = $dom->getElementsByTagName('employeeaddress')->item(0);

//create a new element

$newChild = $dom->createElement('newNode');

//create a text node

$textNode = $dom->createTextNode('newNode');

//append the text to the  newly created element

$newChild->appendChild($textNode);

//replace the employeeaddress element by newly created element

$employee->replaceChild($newChild,$employeeaddress);

echo "<HTML>";

echo "<Head>";

echo "<title>Example of DOMNode->replaceChild</title>";

echo "<Head>";

echo "<body>";

echo "<PRE>";

echo htmlentities($dom->saveXML());

echo "</PRE>";

echo "</body>";

echo "</HTML>";

?>

Output:

The expected output would be:

You can see the employeeaddress element is replaced by newNode.

<?xml version="1.0" encoding="UTF-8"?>
<employee>
      <employeename id="myId">test1</employeename>
      <newNode>newNode</newNode>
      <SSN>11111</SSN>
      <company>XYZ Corporation</company>
</employee>
 



Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
fax server
swimming pool contractor
halloween mask
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP