xml分析demo

package com.xml;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/** 
* 
* @author wangxiao 
* DOM生成与解析XML文档 
*/ 
public class XMLDemo { 
	private static Document document; 
	private String fileName; 
	public static void init() { 
		try { 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			document = builder.newDocument(); 
		} catch (ParserConfigurationException e) { 
			System.out.println(e.getMessage()); 
		} 
	} 
	public static String createXml(String fileName, String ToUserName, String FromUserName, String CreateTime, String MsgType, String Content, String FuncFlag) { 
		Element root = document.createElement("xml"); 
		document.appendChild(root); 
		
		Element toUserName = document.createElement("ToUserName");
		toUserName.appendChild(document.createCDATASection(ToUserName)); 
		root.appendChild(toUserName);

		Element fromUserName = document.createElement("FromUserName");
		fromUserName.appendChild(document.createCDATASection(FromUserName)); 
		root.appendChild(fromUserName);
		
		Element createTime = document.createElement("CreateTime");
		createTime.appendChild(document.createTextNode(String.valueOf(new Date().getTime()))); 
		root.appendChild(createTime);
		
		Element msgType = document.createElement("MsgType");
		msgType.appendChild(document.createCDATASection(MsgType)); 
		root.appendChild(msgType);
		
		Element content = document.createElement("Content");
		content.appendChild(document.createCDATASection(Content)); 
		root.appendChild(content);
		
		Element funcFlag = document.createElement("FuncFlag");
		funcFlag.appendChild(document.createCDATASection(FuncFlag)); 
		root.appendChild(funcFlag);
		
		TransformerFactory tf = TransformerFactory.newInstance(); 
		try { 
			Transformer transformer = tf.newTransformer(); 
			DOMSource source = new DOMSource(document); 
			transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312"); 
			transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer); 
			transformer.transform(source, result); 
			System.out.println("生成XML文件成功!"); 
			return writer.getBuffer().toString();
		} catch (Exception e) { 
			System.out.println(e.getMessage()); 
		}
		return FuncFlag; 
	}
	public static void parserXml(InputStream value) { 
		try { 
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(value); 
			NodeList employees = document.getChildNodes(); 
			for (int i = 0; i < employees.getLength(); i++) { 
				Node employee = employees.item(i); 
				NodeList employeeInfo = employee.getChildNodes(); 
				for (int j = 0; j < employeeInfo.getLength(); j++) { 
					Node node = employeeInfo.item(j); 
					NodeList employeeMeta = node.getChildNodes(); 
					for (int k = 0; k < employeeMeta.getLength(); k++) { 
					System.out.println(node.getNodeName() + ":" + employeeMeta.item(k).getTextContent()); 
					} 
				} 
			} 
			System.out.println("解析完毕"); 
		} catch (FileNotFoundException e) { 
			System.out.println(e.getMessage()); 
		} catch (ParserConfigurationException e) { 
			System.out.println(e.getMessage()); 
		} catch (SAXException e) { 
			System.out.println(e.getMessage()); 
		} catch (IOException e) { 
			System.out.println(e.getMessage()); 
		} 
	} 
//public static void createXml(String fileName, String ToUserName, String FromUserName, String CreateTime, String MsgType, String Content, String FuncFlag) { 	
	public static void main(String[] args) {
//		XMLDemo.init();
//		String str = XMLDemo.createXml("D:/demo2.xml","ToUserName", "FromUserName", "CreateTime", "MsgType", "Content", "FuncFlag");
//		System.out.println(str);
		
//		String line = "<xml><ToUserName><![CDATA[gh_d306a11695ae]]></ToUserName>";
//		 String linenew = line.replace("<![CDATA[", "").replace("]]>", "");
//         
//         String key = linenew.substring(1, linenew.indexOf(">"));
//         String value = linenew.substring(key.length()+2, linenew.indexOf("</"));
//         System.out.println(key+"  "+value);
		String source = "<xml><ToUserName><![CDATA[oRDIJj7TLbdhBGjEknLLS4lJ_KDE]]></ToUserName><FromUserName><![CDATA[gh_d306a11695ae]]></FromUserName><CreateTime>1359049881672</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[this is testtestetsetsetsetet]]></Content><FuncFlag>0</FuncFlag></xml>";
		String key = "Content";
		String result = "";
		
		source = source.replace(" ", "").replace("\n", "").replace("\t", "").replace("<![CDATA[", "").replace("]]>", "");
		String ftag = "<"+key+">";
		String ltag = "</"+key+">";
		if(source.contains(ftag) && source.contains(ltag)) {
			result = source.substring(source.indexOf(ftag)+ftag.length(), source.indexOf(ltag));
		}
		System.out.println(result);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ajax请求的操作步骤: 1. 创建XMLHttpRequest对象 使用JavaScript的XMLHttpRequest对象可以异步地从服务器获取数据。可以通过以下代码创建XMLHttpRequest对象: ``` var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } ``` 2. 定义回调函数 当从服务器获取到数据时,需要执行的操作可以定义在回调函数中。回调函数必须在XMLHttpRequest对象的readyState属性发生改变时被调用。可以使用以下代码定义回调函数: ``` xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // 执行操作 } } ``` 3. 发送请求 使用XMLHttpRequest对象的open()和send()方法发送请求。open()方法指定请求的方法(GET或POST)、URL和是否异步。send()方法向服务器发送请求。例如,以下代码发送一个GET请求: ``` xmlhttp.open("GET", "demo_get.asp", true); xmlhttp.send(); ``` 4. 接收响应 当从服务器获取到响应时,XMLHttpRequest对象的responseText或responseXML属性中将包含响应的内容。responseText属性包含响应的文本内容,responseXML属性包含响应的XML内容。例如,以下代码获取响应的文本内容: ``` xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var responseText = xmlhttp.responseText; // 执行操作 } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值