求解析这段xml字符串,axis调用接口返回的就是这样
本帖最后由 iamaj2eeprogrammer 于 2013-01-23 22:50:42 编辑
求解析这段xml的方法。
<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"><EmployeeID>adminjm</EmployeeID><DepartmentID>822</DepartmentID><EmployeeSign>adminjm</EmployeeSign><EmployeeName>adminjm</EmployeeName><CardID> </CardID><Email>adminjm@jm.gd.chinamobile.com</Email><MobileNo> </MobileNo><WorkTelNo> </WorkTelNo><Address> </Address><Gender>2</Gender><OrderNo> </OrderNo></Table>
<Table diffgr:id="Table2" msdata:rowOrder="1"><EmployeeID>aoyingkang</EmployeeID><DepartmentID>9151</DepartmentID><EmployeeSign>aoyingkang</EmployeeSign><EmployeeName>敖颖康</EmployeeName><CardID>1321700717</CardID><Email>aoyingkang@jm.gd.chinamobile.com</Email><MobileNo>13702225278</MobileNo><WorkTelNo>13929042928</WorkTelNo><Gender>1</Gender><OrderNo>100000</OrderNo></Table>
<Table diffgr:id="Table3" msdata:rowOrder="2"><EmployeeID>caibingbing2</EmployeeID><DepartmentID>9186</DepartmentID><EmployeeSign>caibingbing2</EmployeeSign><EmployeeName>蔡冰冰</EmployeeName><CardID>1321700659</CardID><Email>caibingbing2@jm.gd.chinamobile.com</Email><MobileNo>13827087897</MobileNo><WorkTelNo>13929042540</WorkTelNo><Gender>2</Gender><OrderNo>100000</OrderNo></Table>
</NewDataSet></diffgr:diffgram>
------解决方案--------------------public class Test {
public static void main(String[] args) throws Exception{
SAXReader reader = new SAXReader();
//读取test.xml文件
InputStream in = new FileInputStream("C:\\Users\\Administrator\\Desktop\\test.xml");
Document doc = reader.read(in);
//获取根标签
Element diffgr = doc.getRootElement();
Element newDataSets = diffgr.element("NewDataSet");
List<Element> tables =newDataSets.elements("Table");
for (Element table : tables) {
//Table标签的两个属性
Attribute id = table.attribute("id");
Attribute rowOrder = table.attribute("rowOrder");
//获取属性的值
String idAttr = id.getText();
String rowOrderAttr = rowOrder.getText();
//Table标签里面的各个子标签
Element employeeId = table.element("EmployeeID");
Element departmentId = table.element("DepartmentID");
//省略其他子标签...
//获取标签内的值
String employeeIdText = employeeId.getText();
String departmentIDText = departmentId.getText();
}
}
}