Java creates an XML document from a result set

Source: Internet
Author: User
Package com.hotmail.friendly;

Import java.util.ArrayList;

Import Java.util.Iterator;



Import java.util.List;

Import org.dom4j.Document;

Import org.dom4j.DocumentException;

Import Org.dom4j.DocumentHelper;



Import org.dom4j.Element; 



	public class Xmltest {//private static map<string, string> XmlMap = new hashmap<string,//string> ();

	

	containers that store XML element information private static list<leaf> elemlist = new arraylist<leaf> (); The XML object to be tested private static String Srcxml = "<?xml version=/" 1.0/"encoding=/" gbk/"+" <            doc>/n "+" <person>/n "+" <name> someone </name>/n "+" <adds>



			/n "+" <add id=/"10002/" >/n "+" <bs>10002</bs>/n "+ "<note> Xi ' an city Taibai road </note>/n" + "</add>/n" + "<add I d=/"/" >/n "+" <bs>10002</bs>/n "+" <note> NULL ID node AH </note>/n "+" </add>/n " + "<add>/n" + "<bs>10002</bs>/n" + "<n				 ote> NULL ID node AH </note>/n "+" </add>/n "+" <add id=/"10001/" >/n "+" <bs xmlns=/"10001/"/>/n "+" <note> Xi ' an city Taibai road 2</note>/n "+" </add >/n "+" </adds>/n "+" </person>/n "+" <other>/n "+" <nam

	

	E id=/"hehe/" ">asdf</name>/n" + "</other>/n" + "</doc>"; /** * Get root element * * @return * @throws documentexception/Public element getrootelement () throws Document

		Exception {Document Srcdoc = Documenthelper.parsetext (srcxml);



		Element Elem = Srcdoc.getrootelement ();

	return elem; /** * Recursive Traversal method * * @paraM element */public void Getelementlist (element Element) {List elements = element.elements ();



			if (elements.size () = = 0) {//No child element String XPath = Element.getpath ();



			String value = Element.gettexttrim ();



		Elemlist.add (New Leaf (XPath, value)); else {//has child element for (Iterator it = Elements.iterator (); It.hasnext ();)



				{element elem = (Element) It.next ();

			Recursive traversal getelementlist (elem);

		}} public String getliststring (list<leaf> elemlist) {stringbuffer sb = new StringBuffer (); for (iterator<leaf> it = Elemlist.iterator (); It.hasnext ();)

			{The Leaf leaf = It.next ();

		Sb.append (Leaf.getxpath ()). Append ("="). Append (Leaf.getvalue ()). Append ("/n");

		

	return sb.tostring ();

		

		public static void Main (String args[]) throws documentexception {xmltest test = new Xmltest ();

		

		Element root = Test.getrootelement ();

		

		Test.getelementlist (root);String x = test.getliststring (elemlist);

		

		System.out.println ("-----------Original XML Content------------");

		

		System.out.println (Srcxml);

		

		System.out.println ("-----------analytic result------------");



	SYSTEM.OUT.PRINTLN (x);



}} package com.hotmail.friendly; 



	/** * XML Node data structure * * public class Leaf {private String XPath;



	private String value;



		Public Leaf (String xpath, string value) {This.xpath = XPath;



	This.value = value;



	Public String Getxpath () {return XPath;



	public void Setxpath (String xpath) {this.xpath = XPath;



	Public String GetValue () {return value;



	public void SetValue (String value) {this.value = value;

 }



}

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.