package com.topthinking.tel.test.dom4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import java.util.*;
public class TestSAX {
// private static Map<String, String> xmlmap = new HashMap<String,
// String>();
private static List<Leaf> elemList = new ArrayList<Leaf>();
private static String srcXml =
"<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">" +
"<NewDataSet xmlns=\"\">" +
"<Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\">" +
"<id>125704</id>" +
"<l_name>我暈</l_name>" +
"<m1_number>8613574286112</m1_number>" +
"<local_number>861052231235</local_number>" +
"<m2_name>海,小剛</m2_name>" +
"</Table>" +
"<Table diffgr:id=\"Table2\" msdata:rowOrder=\"1\">" +
"<id>124849</id><l_name>我暈</l_name>" +
"<m1_number>8613574286112</m1_number><local_number>" +
"862151923508</local_number>" +
"<m2_name>jerry217(Internet)</m2_name>" +
"</Table>" +
"<Table diffgr:id=\"Table6\" msdata:rowOrder=\"1\">" +
"<id>124849</id>" +
"<l_name>我暈</l_name>" +
"<m1_number>8613574286112</m1_number>" +
"<local_number>862151923508</local_number>" +
"<m2_name>jerry217(Internet)</m2_name><" +
"/Table>" +
"</NewDataSet>" +
"</diffgr:diffgram>";
//<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:UseCurrentLocale=\"true\"><xs:complexType><xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\"><xs:element name=\"Table\"><xs:complexType><xs:sequence><xs:element name=\"UID\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"Local_Number\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"Destination_Number\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"End_Time\" type=\"xs:dateTime\" minOccurs=\"0\"/><xs:element name=\"Start_Time\" type=\"xs:dateTime\" minOccurs=\"0\"/><xs:element name=\"Opus\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"Amount\" type=\"xs:double\" minOccurs=\"0\"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema>
//<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\"><NewDataSet xmlns=\"\"><Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"><id>125704</id><l_name>我暈</l_name><m1_number>8613574286112</m1_number><local_number>861052231235</local_number><m2_name>海,小剛</m2_name></Table><Table diffgr:id=\"Table2\" msdata:rowOrder=\"1\"><id>124849</id><l_name>我暈</l_name><m1_number>8613574286112</m1_number><local_number>862151923508</local_number><m2_name>jerry217(Internet)</m2_name></Table></NewDataSet></diffgr:diffgram>
//<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\"><NewDataSet xmlns=\"\"><Table diffgr:id=\"Table1\" msdata:rowOrder=\"0\"><UID>8613574286112</UID><Local_Number>86101756751060</Local_Number><Destination_Number>86123342545</Destination_Number><End_Time>2009-01-15T14:03:00+08:00</End_Time><Start_Time>2009-01-05T14:03:00+08:00</Start_Time><Opus>ADD</Opus><Amount>1</Amount></Table></NewDataSet></diffgr:diffgram>
// "<?xml version=\"1.0\" encoding=\"GBK\"?>"
// "<doc>"
// + "<person>"
// + "<name>某人</name>"
// + "<adds>"
// + "<add ID=\"10002\">"
// + "<BS>10002</BS>"
// + "<note>西安市太白路</note>"
// + "</add>"
// + "<add ID=\"\">"
// + "<BS>10002</BS>"
// + "<note>空ID节点啊</note>"
// + "</add>"
// + "<add>"
// + "<BS>10002</BS>"
// + "<note>空ID节点啊</note>"
// + "</add>"
// + "<add ID=\"10001\">"
// + "<BS xmlns=\"10001\"/>"
// + "<note>西安市太白路2</note>"
// + "</add>"
// + "</adds>"
// + "</person>"
// + "<other>"
// + "<name ID=\"HEHE\">ASDF</name>"
// + "</other>"
// + "</doc>";
public static void main(String args[]) throws DocumentException {
TestSAX test = new TestSAX();
Element root = test.getRootElement();
test.getElementList(root);
String x = test.getListString(elemList);
System.out.println("-----------原xml内容------------");
System.out.println(srcXml);
System.out.println("-----------解析结果------------");
System.out.println(x);
}
/**
* 获取根元素
*
* @return
* @throws DocumentException
*/
public Element getRootElement() throws DocumentException {
Document srcdoc = DocumentHelper.parseText(srcXml);
Element elem = srcdoc.getRootElement();
return elem;
}
/**
* 递归遍历方法
*
* @param element
*/
public void getElementList(Element element) {
List elements = element.elements();
// System.out.println("____"+elements);
if (elements.size() == 0) {
// 没有子元素
// System.out.println("gfgf:"+element.getStringValue());
System.out.println(element.getUniquePath());
// System.out.println(element.getStringValue());
String xpath = element.getPath();
String value = element.getTextTrim();
elemList.add(new Leaf(xpath, value));
} else {
// 有子元素
for (Iterator it = elements.iterator(); it.hasNext();) {
Element elem = (Element) it.next();
// 递归遍历
getElementList(elem);
}
}
}
public String getListString(List<Leaf> elemList) {
StringBuffer sb = new StringBuffer();
for (Iterator<Leaf> it = elemList.iterator(); it.hasNext();) {
Leaf leaf = it.next();
sb.append(leaf.getXpath()).append(" = ").append(leaf.getValue())
.append("\n");
}
return sb.toString();
}
}
/**
* xml节点数据结构
*/
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;
}
}