File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes reading/updating child  node attribute in xml String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "reading/updating child  node attribute in xml String" Watch "reading/updating child  node attribute in xml String" New topic
Author

reading/updating child node attribute in xml String

Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 65
Hi,

I have an xml string.
I need to update one of attribute value in that xml String and need to paas an updated string into further code.

On javaranch i found a sample a code to get DOM document object from xml string.
Sample code as found
<url>http://faq.javaranch.com/java/StringToDocument<url>
<code>
public class Util {

public static Document stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(xmlSource)));
}
}<code>

But the problem what i am facing with this is -
the document object it is returning didn't have a method to get the root node like getRootNode().


My task are -
1) Read xml String
2) get the chid node's attribute
3) update that attribute value.
4) updates should be on original string also.

Please help me on this.

Thanks
Sharad
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Sharad Golu wrote:the document object it is returning didn't have a method to get the root node like getRootNode().

It's called getDocumentElement(). The full API documentation is here: org.w3c.dom.Document.
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 65
Hello Paul,
Thanks for your quick response.

But getDocumentElement() return an Element interface.

My sample xml String is-
String strXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><command autoname=\"true\"><cadobjectlist><cadobject type=\"ai\" name=\"test\" /><cadobject parentid=\"3\" /></cadobjectlist></command>";

Now i need to update 'name' attribute value.
from test to new.

If possible can you provide a full code for this.

Tthanks in advance
Sharad

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

No, this isn't a place for getting people to do your work for you. Have a look at the XmlFaq page; scroll down to the "Articles" section. There are several links there to pages on how to write DOM-navigating code.
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 65
Thanks for xmlfaq link, It helps a lot.
I was just asking if you might have a working code, it would be helpful for me as i was struggling hard from last 2 days to resolve this.
any ways below posting a code.
Hope this might help someone else in future with same requirement.

String strXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><command autoname=\"true\"><cadobjectlist><cadobject type=\"ai\" name=\"test\" /><cadobject parentid=\"3\" /></cadobjectlist></command>";
Document doc = StringToDocument(strXml);
updateAttributeBusName(doc);
String newxml = DocumentToString(doc);


public void updateAttributeBusName(Document doc)
{
NodeList cadobjectNodeList = doc.getElementsByTagName("cadobject");
//System.out.println(cadobjectNodeList.getLength());
for(int i=0; i<cadobjectNodeList.getLength(); i++)
{
Element cadObjectElem = (Element)cadobjectNodeList.item(i);
String type = cadObjectElem.getAttribute("type");
String autoBusName = "new";
if("ai".equalsIgnoreCase(type))
{
cadObjectElem.setAttribute("name", autoBusName);
}
}

}
public Document StringToDocument(String strXml) throws Exception
{
Document doc = null;
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader strReader = new StringReader(strXml);
InputSource is = new InputSource(strReader);
doc = (Document)builder.parse(is);
}catch(Exception e)
{
e.printStackTrace();
throw e;
}

return doc;
}

public String DocumentToString(Document doc) throws Exception
{
String strXml = "";
try
{
Node node = doc.getElementsByTagName("command").item(0);
Source source = new DOMSource(node);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
strXml = stringWriter.getBuffer().toString();
}catch(Exception e)
{
e.printStackTrace();
throw e;
}
return strXml;
}

~Sharad
>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading/updating child node attribute in xml String
 
Similar Threads
Fresher ! need help
Retrieve objects from XSL transform
Why I can't retrieve records stored in a XMLFile correctly?
Sample Code For DOM
XML String to Document