Java reads and writes XML using the DOM method

Source: Internet
Author: User

One, Dom read

Import * * *;

public class ReadXML {

public static void Main (string[] args) {
try {
Dom
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
Document document = Builder.parse (new File ("Languages.xml"));
Element root = Document.getdocumentelement ();
System.out.println ("cat=" +root.getattribute ("cat"));
NodeList list = Root.getelementsbytagname ("LAN");
for (int i = 0; i < list.getlength (); i++) {
Element lan = (element) list.item (i);
System.out.println ("---------------");
System.out.println ("id=" +lan.getattribute ("id"));
NodeList clist = Lan.getchildnodes ();
for (int j = 0; J < Clist.getlength (); j + +) {
Node C = Clist.item (j);
if (c instanceof Element) {//The function of this line is to remove elements such as the element Key's carriage return and other invisible elements such as #text
System.out.println (C.getnodename () + "=" +c.gettextcontent ());
}
}
}
} catch (* *) {...}

}

The XML file is as follows

<?xml version= "1.0" encoding= "UTF-8"?>
<languages cat= "It" >
<lan id= "1" >
<name>Java</name>
<ide>Eclipse</ide>
</lan>
<lan id= "2" >
<name>Swift</name>
<ide>Xcode</ide>
</lan>
<lan id= "3" >
<name>C#</name>
<ide>visual studio</ide>
</lan>
</Languages>

Second, Dom creates XML

Dom

Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
Document document = Builder.newdocument ();
Element root = document.createelement ("Languages");
Root.setattribute ("Cat", "it");

Element lan1 = document.createelement ("LAN");
Lan1.setattribute ("id", "1");
Element name1 = document.createelement ("name");
Name1.settextcontent ("Java");
Element ide1 = document.createelement ("IDE");
Ide1.settextcontent ("Eclipse");
Lan1.appendchild (NAME1);
Lan1.appendchild (IDE1);

Element lan2 = document.createelement ("LAN");
Lan2.setattribute ("id", "2");
Element name2 = document.createelement ("name");
Name2.settextcontent ("Swift");
Element Ide2 = document.createelement ("IDE");
Ide2.settextcontent ("XCode");
Lan2.appendchild (name2);
Lan2.appendchild (IDE2);

Element lan3 = document.createelement ("LAN");
Lan3.setattribute ("id", "3");
Element Name3 = document.createelement ("name");
Name3.settextcontent ("C #");
Element ide3 = document.createelement ("IDE");
Ide3.settextcontent ("Visual Studio");
Lan3.appendchild (Name3);
Lan3.appendchild (IDE3);

Root.appendchild (LAN1);
Root.appendchild (LAN2);
Root.appendchild (LAN3);
Document.appendchild (root);

//-------------

Transformerfactory transformerfactory = Transformerfactory.newinstance ();
Transformer Transformer = Transformerfactory.newtransformer ();
Transformer.setoutputproperty ("Encoding", "UTF-8");

StringWriter writer = new StringWriter ();
Transformer.transform (new Domsource (document), new Streamresult (writer));
System.out.println (Writer.tostring ());
Transformer.transform (new Domsource (document), New Streamresult (New File ("Newxml.xml"));

Java reads and writes XML using the DOM method

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.