Sample XML
<?xml version="1.0"?> <connection> <number id="1"></number> <number id="2"></number> <number id="3"></number> </connection>
import javax.xml.namespace.QName; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class ModifyxmlSelf { public static void main(String[] args) { XMLInputFactory ifactory=XMLInputFactory.newFactory(); XMLOutputFactory ofactory=XMLOutputFactory.newFactory(); StreamSource source =new StreamSource("connection.xml"); StreamResult result=new StreamResult("connection.xml"); try { XMLEventReader in=ifactory.createXMLEventReader(source); XMLEventWriter out=ofactory.createXMLEventWriter(result); while(in.hasNext()){ XMLEvent e=in.nextEvent(); if(e.isStartElement() && ((StartElement)e).getName().getLocalPart().equalsIgnoreCase("Number")){ Attribute a=((StartElement)e).getAttributeByName(new QName("id")); if(a.getValue().equalsIgnoreCase("3")){ in.next(); continue; } } out.add(e); } in.close(); out.close(); } catch (XMLStreamException e) { e.printStackTrace(); } } }
output XML
<?xml version="1.0"?> <connection> <number id="1"></number> <number id="2"></number> </connection>
No comments:
Post a Comment