BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
PHP XML
PHP DOMDocument Function setAttribute
PHP DOMDocument Function schemaValidateSource (handling errors)
PHP DOMDocument Function schemaValidateSource
PHP DOMDocument Function schemaValidate
PHP DOMDocument Function saveXML
PHP DOMDocument Function saveHTMLFile
PHP DOMDocument Function saveHTML
PHP DOMDocument Function normalize
PHP DOMDocument Function loadXML
PHP DOMDocument Function loadHTMLFile
PHP DOMDocument Function loadHTML
PHP DOMDocument Function importNode
PHP DOMElement Function hasAttribute
PHP DOMDocument Function getElementsByTagName
PHP DOMDocument Function getAttributeNS
PHP DOMDocument Function getAttributeNodeNS
PHP DOMDocument Function getAttribute
PHP DOMDocument Function createTextNode
PHP DOMDocument Function createProcessingInstruction
PHP DOMDocument Function createEntityReference
 
 

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 :1072

 

DOMDocument Function setAttributeNS

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

DOM: W3C DOM Level 1

Description

This function sets the name space of the attribute. If the attribute does not exists, it creates the attribute with name space.

Usage:

voidDOMElement->setAttributeNS ( string namespaceURI, string qualifiedName, string value)

Sets an attribute with namespace namespaceURI and name name to the given value. If the attribute does not exist, it will be created.

Throws DOMException if node cannot be modified.

PHP Code:

<?php

$xmlstr = <<<XML

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

<employee>

            <employeename id="myId">test</employeename>

            <employeeaddress>test123</employeeaddress>

            <SSN>12343456</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 employeeaddress

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

//set the namespace of the street attribute. If this attribute does not exist, it will create the attribute

$employeeaddr ->setAttributeNS('myNS','prefix:street','times square');

//save the modified DOM Document into a file

$dom->save("C:/topxml_demo/php/xml_files/save1.xml");

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

//retrive the attribute node

$attributeNode = $employeename->getAttributeNode('id');

//echo out the content

echo "<HTML>";

echo "<Head>";

echo "<title>Example of setAttributeNS </title>";

echo "<Head>";

echo "<body>";

echo "<B>";

echo "Attribute created with Namespace";

echo "</B>";

echo "<body>";

echo "<HTML>";

?>

Output:

Content of save1.xml file :

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

<employee>

     <employeename id="myId">test</employeename>

     <employeeaddressxmlns:prefix="myNS"prefix:street="times square">test123</employeeaddress>

     <SSN>12343456</SSN>

     <company>XYZ Corporation</company>

</employee>

Note:

Ensure that if attribute nodes have their own "xmlns" attributes, then they have to be defined with a prefix (if there is no prefix defined for this namespace!). Otherwise it will throw exception.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Integration Specialist Needed - Wor
Virtualization Server Infrastructur
A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En

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




Web Hosting
TMobile G1 Accessories
medical tourism procedures


    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