@SuppressWarnings("unchecked")
public XPath getXPath(Document doc, String path) {
XPath xpath = null;
try {
xpath = doc.createXPath(path);
} catch (InvalidXPathException e) {
logger.info("路径[" + path + "]不合法,请检查报文路径后重试!");
}
Map<String, String> mpNameSpace = new HashMap<String, String>();
List<Namespace> declareNamespaces = doc.getRootElement().declaredNamespaces();
for (Namespace ns : declareNamespaces) {
if (ns.getPrefix() != null && ns.getPrefix().length() > 0) {
mpNameSpace.put(ns.getPrefix(), ns.getURI());
} else {
mpNameSpace.put("default", ns.getURI());
}
}
if (xpath != null) {
xpath.setNamespaceURIs(mpNameSpace);
}
return xpath;
}
@SuppressWarnings("unchecked")
public Document getPackageAllContent(Document doc) {
List<Element> elementList = doc.selectNodes("/aaa/bbb");
Document change_doc = null;
if (elementList.size() > 0) {
List<Element> result = new LinkedList<Element>();
for (Element ele : elementList) {
List<Element> cel = ele.elements();
if (cel != null) {
for (Element ce : cel) {
result.add(ce);
}
}
}
String xml = "";
try {
xml = result.get(0).asXML();
change_doc = DocumentHelper.parseText(xml);
} catch (DocumentException e) {
logger.info("报文[" + xml + "]不合法,请检查报文后重试!");
}
} else {
change_doc = doc;
}
return change_doc;
}