首先得到:得到 DOM 

DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();

然后从 DOM 工厂获得 DOM

DocumentBuilder dombuilder=domfac.newDocumentBuilder();

3 )把要解析的XML,以便 DOM

InputStream is=  new  FileInputStream("test1.xml");

( 4 )解析 XML 文档的输入流,得到一个 Document

Document doc=dombuilder.parse(is);

( 5 )得到 XML

Element root=doc.getDocumentElement();

( 6

  NodeList books=root.getChildNodes();

名称

说明


StringReader(String)


初始化从指定字符串进行读取的 StringReader


构造方法摘要

InputSource() 

          零参数默认构造方法。

InputSource(InputStream byteStream) 

          使用字节流创建新的输入源。

InputSource(Reader characterStream) 

          使用字符流创建新的输入源。

InputSource(String systemId) 

          使用系统标识符创建新的输入源。

package cn.com.test;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import cn.com.yeexunwechat.encoderUtil.WXBizMsgCrypt;


public class Program {

	public static void main(String[] args) throws Exception {

		//
		// 第三方回复公众平台
		//

		// 需要加密的明文
		String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
		String token = "pamtest";
		String timestamp = "1409304348";
		String nonce = "xxxxxx";
		String appId = "wxb11529c136998cb6";
		String replyMsg = " 中文<xml><ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";

		WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
		String mingwen = pc.encryptMsg(replyMsg, timestamp, nonce);
		System.out.println("加密后: " + mingwen);

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		StringReader sr = new StringReader(mingwen);
		InputSource is = new InputSource(sr);
		Document document = db.parse(is);

		Element root = document.getDocumentElement();
		NodeList nodelist1 = root.getElementsByTagName("Encrypt");
		NodeList nodelist2 = root.getElementsByTagName("MsgSignature");

		String encrypt = nodelist1.item(0).getTextContent();
		String msgSignature = nodelist2.item(0).getTextContent();

		String format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
		String fromXML = String.format(format, encrypt);

		//
		// 公众平台发送消息给第三方,第三方处理
		//

		// 第三方收到公众号平台发送的消息
		String result2 = pc.decryptMsg(msgSignature, timestamp, nonce, fromXML);
		System.out.println("解密后明文: " + result2);
		
		//pc.verifyUrl(null, null, null, null);
	}
}

Java代码

1. package
2.   
3. import
4. import
5. import
6.   
7. import
8. import
9.   
10. import
11. import
12. import
13. import
14.   
15. public class
16. public static void
17. new
18.     }   
19. public
20.         DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();   
21. try
22.             DocumentBuilder domBuilder = domfac.newDocumentBuilder();   
23. new FileInputStream(new File("D:/test1.xml"));   
24.             Document doc = domBuilder.parse(is);   
25.             Element root = doc.getDocumentElement();   
26.             NodeList books = root.getChildNodes();   
27. if(books!=null){   
28. for (int i = 0; i < books.getLength(); i++) {   
29.                     Node book = books.item(i);   
30. if(book.getNodeType()==Node.ELEMENT_NODE) {   
31. //(7)取得节点的属性值 
32. "email").getNodeValue();   
33.                             System.out.println(email);   
34. //注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE 
35. //(8)轮循子节点 
36. for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {   
37. if(node.getNodeType()==Node.ELEMENT_NODE) {   
38. if(node.getNodeName().equals("name")) {   
39.                                         String name=node.getNodeValue();   
40.                                         String name1=node.getFirstChild().getNodeValue();   
41.                                         System.out.println(name);   
42.                                         System.out.println(name1);   
43.                                     }   
44. if(node.getNodeName().equals("price")) {   
45.                                         String price=node.getFirstChild().getNodeValue();   
46.                                         System.out.println(price);   
47.                                     }   
48.                                 }   
49.                             }   
50.                      }   
51.                 }   
52.             }   
53. catch
54. // TODO Auto-generated catch block 
55.             e.printStackTrace();   
56.         }   
57.            
58.     }   
59. }

Xml代码

1. <?xml version="1.0" encoding="GB2312" standalone="no"?>
2. <books>  
3.     <book email="zhoujunhui">
4.         <name>rjzjh</name>  
5.         <price>jjjjjj</price>  
6.     </book>  
7. </books>

名称

说明


StringReader(String)


初始化从指定字符串进行读取的 StringReader