Blame view

src/com/ectrip/cyt/exceptionsave/xml/Dom4JXMLUtils.java 3.37 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  package com.ectrip.cyt.exceptionsave.xml;
  
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  
  
  import org.dom4j.Attribute;
  import org.dom4j.Document;
  import org.dom4j.DocumentException;
  import org.dom4j.DocumentHelper;
  import org.dom4j.Element;
  import org.dom4j.Node;
  import org.dom4j.io.SAXReader;
  import org.w3c.dom.NodeList;
  
  
  public class Dom4JXMLUtils {
  
  	public static String getPathText(String xmlStr,String strpath)
  	{
  		Document doc = null;
  		String result = null;
  		try {
  			doc = DocumentHelper.parseText(xmlStr);
  			Element root=doc.getRootElement();
  			Node xpath=(Node) root.selectObject(strpath);
  			result=xpath.getText();
  		} catch (DocumentException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  		return result;
  	}
  	public static HashMap<String,String> getPathText(String xmlStr,String[] strpath)
  	{
  		Document doc = null;
  		HashMap<String,String> result = null;
  		System.out.println("AA="+xmlStr);
  		try {
  			doc = DocumentHelper.parseText(xmlStr);
  			Element root=doc.getRootElement();
  			int length=strpath.length;
  			result=new HashMap<String,String>();
  			for(int i=0;i<length;i++)
  			{
  				Node xpath=(Node) root.selectObject(strpath[i]);
  				result.put(strpath[i],xpath.getText());
  			}
  			System.out.println("AA="+xmlStr);
  		} catch (DocumentException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  		return result;
  	}
  
  	public static List<Node> getPathNodeList(String xmlStr,String strpath)
  	{
  		Document doc = null;
  
  		try {
  			doc = DocumentHelper.parseText(xmlStr);
  			Element root=doc.getRootElement();
  
  
  			return (List<Node>) root.selectObject(strpath);
  
  
  		} catch (DocumentException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  		return null;
  	}
  //	public String getPath(String xmlStr,String path)
  //	{
  //		Document doc = null;
  //		try {
  //			doc = DocumentHelper.parseText(xmlStr.replace("\\?", ""));
  //			Element rootElt = doc.getRootElement();
  //			readNode(rootElt, null);
  //		} catch (DocumentException e) {
  //			// TODO Auto-generated catch block
  //			e.printStackTrace();
  //		}
  //		return path;
  //	}
  
  //	static String xPathNode(Document doc,XPathFactory factory,String strpath) {
  //		XPath xpath = factory.newXPath();
  //	
  //		try {
  //			XPathExpression expr = xpath.compile(strpath);
  //			NodeList nodes = (NodeList) expr.evaluate(doc,XPathConstants.NODESET);
  //			return nodes.item(0).getNodeValue();
  //		} catch (XPathExpressionException e) {
  //			// TODO Auto-generated catch block
  //			e.printStackTrace();
  //		}
  //		return strpath;
  //	}
  
  
  	//遍历所有的节点
  	public static void readNode(Element root, String prefix) {
  		if (root == null)
  			return;
  		// 获取属性
  		@SuppressWarnings("unchecked")
  		List<Attribute> attrs = root.attributes();
  		if (attrs != null && attrs.size() > 0) {
  			System.err.print(prefix);
  			for (Attribute attr : attrs) {
  				System.err.print(attr.getValue() + " ");
  			}
  			System.err.println();
  		}
  		// 获取他的子节点
  		List<Element> childNodes = root.elements();
  		prefix += "\t";
  		for (Element e : childNodes) {
  			readNode(e, prefix);
  		}
  	}
  	//读取流
  	public static Element loadData(InputStream in) {
  		try {
  			SAXReader reader = new SAXReader();
  			Document doc = reader.read(in);
  			Element root = doc.getRootElement();
  			return root;
  		} catch (DocumentException e) {
  			e.printStackTrace();
  		}
  		return null;
  
  	}
  
  
  }