[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: adding new attribute to an existing xml fileusing JAXPwith


add node to existing doc
Hi Rahil,

I don't see anything wrong in your code.

This may be a stupid question, but it is better for conform:

Did you finally persist the DOM tree back to test.xml after modifications?

Thanks,
Prakash



Rahil wrote:
Hi

This is my first post to this forum and Im hoping to get some suggestions as Im completely exhausted trying to resolve my query.

My query is to add a new attribute to a selected node in an existing xml file.

I have a test.xml file whose structure is such:

<Top>
   <Tags>
        <Tag id="123" name="Home">
           <Value>Inner Text</Value>
       </Tag>
   </Tags>
</Top>

Now using the JAXP DocumentBuilder I am able to access the attribute @name from the <Tag> node. I then want to insert a new attribute @new="some value" to this <Tag> node. But despite trying out several options Im not able to. I want to do something similar with the <Value> node as well and append some InnerText to the existing one.

My code so far is:
--------------------------------
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(test.xml);

NodeList list = doc.getElementsByTagName("Tag");

for(int i=0; i<list.getLength();i++){
Element tagNode = (Element)list.item(i); //<Tag>
nm = tagNode.getAttributes();

for(int j=0;j<nm.getLength();j++){
   Node ndNm = nm.item(j); // id, name --attributes names
   nodeLocNm = ndNm.getNodeName();

   if(nodeLocNm.equals(new String("name"))){
       String nodeVal = nm.item(j).getNodeValue(); //@name value -- do something
       String newRes = "new value";
       tagNode.setAttribute("new", "new value");
   }
}
}

--------------------------------------------------

Of course no attribute @name gets added to test.xml which is my problem !

Could someone please help.

Thanks in advance to everyone for helping out!
Rahil


-----------------------------------------------------------------
The xml-dev list is sponsored by XML.org http://www.xml.org, an
initiative of OASIS http://www.oasis-open.org

The list archives are at http://lists.xml.org/archives/xml-dev/

To subscribe or unsubscribe from this list use the subscription
manager: http://www.oasis-open.org/mlmanage/index.php


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.