Generate XML and Parse

Source: Internet
Author: User
Tags stringbuffer
Package Com.hanyang;

Import Java.io.BufferedReader;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.StringReader;
Import Java.io.StringWriter;
Import java.io.UnsupportedEncodingException;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;

Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
Import Org.dom4j.io.DOMReader;
Import Org.dom4j.io.OutputFormat;
Import Org.dom4j.io.SAXReader;
Import Org.dom4j.io.XMLWriter;
Import Org.junit.Test;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;

public class Xmlhandler {
@Test
public void Createxml () throws IOException {
The given value
String[] strings = {"User occupied", "CPU Idle"};
Map
map<string, string> map = new hashmap<string, string> ();
Map.put ("1", "user occupied");
Map.put ("2", "System occupied");
Map.put ("3", "CPU idle");
Map.put ("4", "hdd read rate");
If a given value is included, Traverse key value
if (Map.values (). Contains (strings)) {
I looked at the writing
}

Element root1 = documenthelper.createelement ("response");
Document document = Documenthelper.createdocument (ROOT1);
Add a child node to the root node
Element root = root1.addelement ("load");
Element element1 = root.addelement ("Disk_load");
Element1.addelement ("Disk_read"). AddText ("corresponding content");
Element1.addelement ("Disk_write"). AddText ("corresponding content");
Element1.addelement ("Iowait"). AddText ("corresponding content");

Element Element2 = root.addelement ("Disk_storage_load");
Element2.addelement ("Total_storage"). AddText ("corresponding content");

Element Element3 = root.addelement ("Net_load");
Element3.addelement ("Net_receive"). AddText ("corresponding content");
Element3.addelement ("Net_send"). AddText ("corresponding content");

Element element4 = root.addelement ("Memory_load");
Element4.addelement ("Mem_total"). AddText ("corresponding content");
Element4.addelement ("Men_available"). AddText ("corresponding content");
Element4.addelement ("Cached_num"). AddText ("corresponding content");

Element element5 = root.addelement ("Cpu_load");
Element5.addelement ("User_time"). AddText (Map.get ("1"));
Element5.addelement ("System_time"). AddText (Map.get ("2"));
Element5.addelement ("Idle"). AddText (Map.get ("3"));

Storing the generated XML document on the hard disk true indicates whether line wrapping
OutputFormat format = new OutputFormat ("", true);
Format.setencoding ("UTF-8");//Set encoding format
XMLWriter XMLWriter = new XMLWriter (New FileOutputStream ("Person.xml"),
format);
Xmlwriter.write (document);
Xmlwriter.close ();
}

@Test
public void ReadXml () throws Documentexception, IOException {
Map map = new HashMap ();
Document doc = null;
InputStream in = null;
try {
File File = new file ("Person.xml");
List<string> lines=new arraylist<string> ();
BufferedReader br=new BufferedReader (New InputStreamReader (new FileInputStream (file), "UTF-8"));
String line = null;
while (line = Br.readline ()) = null) {
Lines.add (line);
}
Br.close ();
for (String string:lines) {
System.out.println (string);
}
Saxreader reader = new Saxreader ();
Read file converted to document
doc = Reader.read (new File ("person.xml"));
Element Rootelt = Doc.getrootelement (); Get root node
SYSTEM.OUT.PRINTLN ("root node:" + Rootelt.getname ()); Get the name of the root node
Iterator iter = rootelt.elementiterator ("load"); Gets the child node under the root node lead
SYSTEM.OUT.PRINTLN (ITER);
Traverse the lead node
while (Iter.hasnext ()) {
Element Recordele = (Element) Iter.next ();
Iterator iters = Recordele.elementiterator ("Disk_load"); Gets the child node under the head of the child node Disk_load
while (Iters.hasnext ()) {
Element Itemele = (Element) Iters.next ();
String Disk_read = Itemele.elementtexttrim ("Disk_read");
String disk_write = Itemele.elementtexttrim ("Disk_write");
String iowait = Itemele.elementtexttrim ("iowait");
System.out.println ("Disk_read:" + disk_read);
System.out.println ("Disk_write:" + disk_write);
Map.put ("Disk_read", Disk_read);
Map.put ("Disk_write", disk_write);
Map.put ("iowait", iowait);
}
}
} finally {

}
}

private void Xml2csv (InputStream in) {
StringBuffer rs = new StringBuffer ();
Saxreader Saxreader = new Saxreader ();
Document doc = null;
try {
doc = Saxreader.read (in);
list<element> ls = doc.getrootelement (). elements ();
for (int i = 0; ls! = null && ls.size () > i; i++) {
Iterator<element> it = ls.get (i). Elementiterator ();

StringBuffer row = new StringBuffer ();
StringBuffer Header = new StringBuffer ();

while (It.hasnext ()) {
Element el = It.next ();
if (i = = 0) {
Header
Header.append (El.getname ());
if (It.hasnext ()) {
Header.append (",");
}
}
Row.append (El.gettexttrim ());
if (It.hasnext ()) {
Row.append (",");
}
}
Row.append ("\ n");

if (header.length () > 0) {
Header.append ("\ n");
Rs.append (header);
}

Rs.append (row);
}
} catch (Exception e) {
E.printstacktrace ();
}

}

}

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.