In this example, we demonstrate the use of DOM parser to modify an existing XML file :
File : file.xml – Original XML file.
Later, update above XML file via DOM XML Parser.
- Add a new element
- Update existing element attribute
- Update existing element value
- Delete existing element
1. XML file
See before and after XML file.File : file.xml – Original XML file.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <company> <staff id="1"> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> </company>
- Update the staff attribute id = 2
- Update salary value to 200000
- Append a new “age” element under staff
- Delete “firstname” element under staff
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <company> <staff id="2"> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>2000000</salary> <age>28</age> </staff> </company>
2. DOM Parser
DOM XML parser to update an above XML file.import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ModifyXMLFile { public static void main(String argv[]) { try { String filepath = "c:\\file.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(filepath); // Get the root element Node company = doc.getFirstChild(); // Get the staff element , it may not working if tag has spaces, or // whatever weird characters in front...it's better to use // getElementsByTagName() to get it directly. // Node staff = company.getFirstChild(); // Get the staff element by tag name directly Node staff = doc.getElementsByTagName("staff").item(0); // update staff attribute NamedNodeMap attr = staff.getAttributes(); Node nodeAttr = attr.getNamedItem("id"); nodeAttr.setTextContent("2"); // append a new node to staff Element age = doc.createElement("age"); age.appendChild(doc.createTextNode("28")); staff.appendChild(age); // loop the staff child node NodeList list = staff.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); // get the salary element, and update the value if ("salary".equals(node.getNodeName())) { node.setTextContent("2000000"); } //remove firstname if ("firstname".equals(node.getNodeName())) { staff.removeChild(node); } } // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filepath)); transformer.transform(source, result); System.out.println("Done"); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (SAXException sae) { sae.printStackTrace(); } } }
jianbin1205
ReplyDeletecheap uggs
north face outlet online
coach outlet online
uggs outlet
snapback hats
true religion outlet,true religion jeans,true religion kids,true religion jeans sale,true religion jeans for men,true religion jacket,true religion sale
cheap oakley sunglasses
moncler coats
ugg boots on sale
cyber monday
kobe bryants shoes 2015
polo ralph lauren uk
moncler outlet
lululemon outlet
prada outlet
chanel handbags
ugg uk outlet
toms outlet
canada goose outlet
the north face jackets
new england patriots
chicago blackhawks
michael kors outlet sale
new york giants
air force one shoes
nike air max 90
ugg boots
air jordan 11 free shipping
michael kors handbags
nike trainers uk
air jordan shoes
kansas city chiefs
chicago bulls
Burberry Outlet
ReplyDeleteOakley Eyeglasses Michael Kors Outlet Coach Factory Outlet Coach Outlet Online Coach Purses Kate Spade Outlet Toms Shoes North Face Outlet Coach Outlet Gucci Belt North Face Jackets Oakley Sunglasses Toms Outlet North Face Outlet Nike Outlet Nike Hoodies Tory Burch Flats Marc Jacobs Handbags Jimmy Choo Shoes Jimmy Choos
Burberry Belt Tory Burch Boots Louis Vuitton Belt Ferragamo Belt Marc Jacobs Handbags Lululemon Outlet Christian Louboutin Shoes True Religion Outlet Tommy Hilfiger Outlet
Michael Kors Outlet Coach Outlet Red Bottoms Kevin Durant Shoes New Balance Outlet Adidas Outlet Coach Outlet Online Stephen Curry Jersey
Nice post thank you for posting.
ReplyDeletevans shoes
ReplyDeletejordan shoes
nike air max 97
yeezy
jordan retro
yeezy boost 350
kyrie shoes
air max 2018
kd shoes
supreme new york
o0w32s4p56 c6y03n7p33 f0e68e5w46 b7i87o7z68 j1c94t0z19 b5j11u8q33
ReplyDelete