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
>