📄 xmlanalyse.java
字号:
package com.khan.xml;
import java.io.*;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLAnalyse {
public XMLAnalyse() {
}
public static void main(String args[]) {
//byte[] buf = new byte[255];
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://www.monternet.com/dsmp/schemas/\">" +
"<SOAP-ENV:Header>" +
"<TransactionID xmlns=\"http://www.monternet.com/dsmp/schemas/\" xsi:type=\"xsd:string\">0</TransactionID>" +
"</SOAP-ENV:Header>" +
"<SOAP-ENV:Body><SyncOrderRelationResp xmlns=\"http://www.monternet.com/dsmp/schemas/\">" +
"<Version>1.5.0</Version>" +
"<MsgType>SyncOrderRelationResp</MsgType>" +
"<hRet>122</hRet>" +
"</SyncOrderRelationResp>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
HashMap hm = new HashMap();
hm = XMLAnalyse.parseSOAP(str);
for (int i = 0; i < hm.size(); i++) {
System.out.println(hm.keySet().toArray()[i] + "\t\t" + hm.get(hm.keySet().toArray()[i]));
}
}
public static HashMap parseSOAP(Object src) {
HashMap hm = new HashMap();
String xml;
if (src instanceof byte[]) {
xml = new String((byte[]) src);
} else {
xml = src.toString();
}
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder doc = null;
try {
doc = df.newDocumentBuilder();
} catch (ParserConfigurationException e) {
}
Document document = null;
StringReader sr = new StringReader(xml);
try {
document = doc.parse(new InputSource(sr));
} catch (DOMException dom) {
dom.printStackTrace();
} catch (SAXException ioe) {
ioe.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = document.getDocumentElement();
for (int i = 0; i < root.getChildNodes().getLength(); i++) {
NodeList nl = root.getChildNodes().item(i).getChildNodes();
if (nl.getLength() > 0) {
//System.out.println(nl.getLength());
for (int j = 0; j < nl.getLength(); j++) {
String name = nl.item(j).getNodeName();
if (nl.item(j).getLastChild() != null) { // 回车和空格会产生的nodename "#text"
NodeList nlc = nl.item(j).getChildNodes();
if (nlc.getLength() > 1) {
for (int k = 0; k < nlc.getLength(); k++) {
String node_name = nlc.item(k).getNodeName();
if (nlc.item(k).getLastChild() != null) {
if ((nlc.item(k).getLastChild()) != null) {
NodeList nlcc = nlc.item(k).getChildNodes();
if (nlcc.getLength() > 1) {
for (int m = 0; m < nlcc.getLength();m++) {
if (nlcc.item(m).getLastChild() != null) {
if ((node_name.equals("FeeUser_ID") &&nlcc.item(m).getNodeName().equals("MSISDN")) ||(node_name.equals("DestUser_ID") &&nlcc.item(m).getNodeName().equals("MSISDN"))) {
hm.put(node_name,nlcc.item(m).getLastChild().getNodeValue());
}
}
}
} else {
hm.put(node_name,nlc.item(k).getLastChild().getNodeValue());
}
}
}
}
} else {
hm.put(name, nl.item(j).getLastChild().getNodeValue());
}
}
}
}
}
return hm;
}
public static HashMap parseSOAP(InputStream inputstream) {
HashMap hm = new HashMap();
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder doc = null;
try {
doc = df.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document = null;
try {
document = doc.parse(inputstream);
} catch (DOMException dom) {
dom.printStackTrace();
} catch (SAXException ioe) {
ioe.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = document.getDocumentElement();
for (int i = 0; i < root.getChildNodes().getLength(); i++) {
NodeList nl = root.getChildNodes().item(i).getChildNodes();
if (nl.getLength() > 1) {
for (int j = 0; j < nl.getLength(); j++) {
String name = nl.item(j).getNodeName();
if (nl.item(j).getLastChild() != null) { // 回车和空格会产生的nodename "#text"
NodeList nlc = nl.item(j).getChildNodes();
if (nlc.getLength() > 1) {
for (int k = 0; k < nlc.getLength(); k++) {
String node_name = nlc.item(k).getNodeName();
if (nlc.item(k).getLastChild() != null) {
if ((nlc.item(k).getLastChild()) != null) {
NodeList nlcc = nlc.item(k).getChildNodes();
if (nlcc.getLength() > 1) {
for (int m = 0; m < nlcc.getLength();m++) {
if (nlcc.item(m).getLastChild() != null) {
if ((node_name.equals("FeeUser_ID") &&nlcc.item(m).getNodeName().equals("MSISDN")) ||(node_name.equals("DestUser_ID") &&nlcc.item(m).getNodeName().equals("MSISDN"))) {
hm.put(node_name,nlcc.item(m).getLastChild().getNodeValue());
}
}
}
} else {
hm.put(node_name,nlc.item(k).getLastChild().getNodeValue());
}
}
}
}
} else {
hm.put(name, nl.item(j).getLastChild().getNodeValue());
}
}
}
}
}
return hm;
}
public static int parseXml(InputStream inputstream) {
DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
DocumentBuilder doc = null;
try {
doc = df.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document = null;
try {
document = doc.parse(inputstream);
} catch (DOMException dom) {
dom.printStackTrace();
} catch (SAXException ioe) {
ioe.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element root = document.getDocumentElement();
for (int i = 0; i < root.getChildNodes().getLength(); i++) {
NodeList nl = root.getChildNodes().item(i).getChildNodes();
int len = nl.getLength();
if (len > 1) {
System.out.println("处理sp名单");
for (int j = 0; j < len; j++) {
String name = nl.item(j).getNodeName();
if (!name.equals("#text")) { //回车和空格会产生的nodename "#text"
System.out.println("name:" + name + " value:" +nl.item(j).getLastChild().getNodeValue());
}
}
}
}
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -