用JDom生成和解析字符型的XML格式内容

package com.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
 * 用JDom生成和解析字符型的XML格式内容
 * @author rzy
 * @version 1.0
 * */
public class JDomXmlDoc
{
	/**
	 * 下行信息MT请求
	 * @param username 用户名
	 * password 密码
	 * transcationID 通信ID
	 * msg_time	信息时间
	 * msgID 信息唯一ID
	 * Msg_type 信息类型
	 * Src_addr 接收信息的通道号
	 * Dst_addr 手机号码
	 * LinkID linkid
	 * ServiceIOD 产品ID
	 * Content 下行信息内容
	 * pushurl url地址
	 * @return docXml xml格式的下行信息MT请求
	 * */
	public String MTSubmitReq(String username,String password,String transcationID,String msg_time,String msgID,String Msg_type,String Src_addr,String Dst_addr,String LinkID,String ServiceIOD,String content,String pushurl)
	{
		Document doc = new Document();
		Element root = new Element("root");
		root.addContent(new Element("username").setText(username));
		root.addContent(new Element("password").setText(password));
		root.addContent(new Element("transcationID").setText(transcationID));
		root.addContent(new Element("msg_time").setText(msg_time));
		root.addContent(new Element("msgID").setText(msgID));
		root.addContent(new Element("Msg_type").setText(Msg_type));
		root.addContent(new Element("Src_addr").setText(Src_addr));
		root.addContent(new Element("Dst_addr").setText(Dst_addr));
		root.addContent(new Element("LinkID").setText(LinkID));
		root.addContent(new Element("ServiceIOD").setText(ServiceIOD));
		root.addContent(new Element("content").setText(content));
		root.addContent(new Element("pushurl").setText(pushurl));
		doc.addContent(root);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintWriter pw = new PrintWriter(baos);  //用PrintWriter可以解决生成编码为UTF-8格式的XML内容出现中文乱码的问题
		Format format = Format.getPrettyFormat();
		format.setEncoding("UTF-8");
		XMLOutputter docWiter = new XMLOutputter();
		docWiter.setFormat(format);
		try {
			docWiter.output(doc, pw);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String docXml = baos.toString();        
        return docXml;
	}
	/**
	 * 解析字符格式的xml内容
	 * @param docXml 字符格式的xml内容
	 * */
	public void MTSubmitRsp(String docXml)
	{
		SAXBuilder builder = new SAXBuilder();
		StringReader sr = new StringReader(docXml);
		Document doc = new Document();
		try {
			doc = builder.build(sr);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element foo = doc.getRootElement();
		List list = foo.getChildren();
		for(int i = 0;i<list.size();i++)
		{
			Element el = (Element) list.get(i);
			System.out.println(el.getName()+":"+el.getText());
		}
	}
       //示例测试
	public static void main(String[] args)
	{
		JDomXmlDoc mxt = new JDomXmlDoc();
		String str = mxt.MTSubmitReq("admin", "123456", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12");
		System.out.println(str);
		mxt.MTSubmitRsp(str);
	}
}

生成xml格式内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <username>admin</username>
  <password>123456</password>
  <transcationID>3</transcationID>
  <msg_time>4</msg_time>
  <msgID>5</msgID>
  <Msg_type>6</Msg_type>
  <Src_addr>7</Src_addr>
  <Dst_addr>8</Dst_addr>
  <LinkID>9</LinkID>
  <ServiceIOD>10</ServiceIOD>
  <content>11</content>
  <pushurl>12</pushurl>
</root>
解析xml格式内容如下:
username:admin
password:123456
transcationID:3
msg_time:4
msgID:5
Msg_type:6
Src_addr:7
Dst_addr:8
LinkID:9
ServiceIOD:10
content:11
pushurl:12

你可能感兴趣的:(java,xml)