Java Operation XML

Source: Internet
Author: User

XML file
<template><task id= "DDD" >aaa</task></template>

Building the DOM
Import Javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import Org.w3c.dom.Document,-----------------------documentbuilderfactory factory = Documentbuilderfactory.newinstance () ;D Ocumentbuilder builder = factory.newdocumentbuilder ();D ocument doc = Builder.parse (This.getclass (). getResourceAsStream ("/template.xml"));//This refers to the SRC directory
add element
Element element = doc.createelement ("task");//Create Elements Element.setattribute ("id", "AAA");//Add Attribute Element.appendchild ( Doc.createtextnode ("DDD"));//Add Text node Doc.getdocumentelement (). appendchild (element);//append to the root node element transformerfactory Tfactory =transformerfactory.newinstance (); Transformer Transformer = Tfactory.newtransformer ();D omsource Source = new Domsource (DOC); Streamresult result = new Streamresult (New FileOutputStream (This.getclass (). GetResource ("/template.xml"). GetPath ()) ); Transformer.transform (source, result);//write to File
Delete Element
Doc.getdocumentelement (). RemoveChild (Doc.getelementsbytagname ("task"). Item (0));// Delete the first task element in the root node transformerfactory tfactory =transformerfactory.newinstance (); Transformer Transformer = Tfactory.newtransformer (); Msource Source = new Domsource (DOC); Streamresult result = new Streamresult (New FileOutputStream (This.getclass (). GetResource ("/template.xml"). GetPath ()) ); Transformer.transform (source, result);//write to File
modifying elements
Doc.getelementsbytagname ("task"). Item (0). Getfirstchild (). setNodeValue ("CCC");// Modifies the contents of the first task element transformerfactory tfactory =transformerfactory.newinstance (); Transformer Transformer = Tfactory.newtransformer (); Omsource Source = new Domsource (DOC); Streamresult result = new Streamresult (New FileOutputStream (This.getclass (). GetResource ("/template.xml"). GetPath ()) ); Transformer.transform (source, result);//write to File
viewing elements
NodeList tasks = Doc.getdocumentelement (). Getchildnodes (); for (int i = 0;i < Tasks.getlength (); i++) { System.out.println ((Element) Tasks.item (i)). getattribute ("id"));//The id attribute value of the output element System.out.println (Tasks.item (i). Getnodevalue);//output element's content value}


Java Operation XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.