• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading/updating child node attribute in xml String

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
>
 
reply
    Bookmark Topic Watch Topic
  • New Topic