关于xml解析和组装
public class XmlUtil { public static boolean xSetValue(Document myDocument, String path, String value) {/* * 对path进行合法性校验 它必须满足是"/"打头的,并且最后一个字符应该是英文字母或者数字。 去掉第一个字符"/" */if (!path.startsWith("/") || path.endsWith("/") || path.length() < 1) {return false;} else {path = path.substring(1);}String[] items = path.split("/");Element parentElement = myDocument.getRootElement();for (int i = 0; i < items.length; i++) {/* * 获取当前节点如果是第一个元素,那么它的父节点是root */Element obj = null;List childList;String id = "";String itemIndex = "0";//存在同名节点情况int index1 = items[i].indexOf("|");if (index1 > 0) {itemIndex = items[i].substring(index1 + 1);items[i] = items[i].substring(0, index1);}//存在属性名情况int index2 = items[i].indexOf("<");int index3 = items[i].indexOf(">");if (index2 > 0 && index3 > 0 && index2 < index3) {id = items[i].substring(index2 + 1, index3);items[i] = items[i].substring(0, index2);}childList = parentElement.getChildren(items[i]);if (childList.size()<=0) {obj = new Element(items[i]);if (id.indexOf("=") > 0) {String item_key = id.substring(0, id.indexOf("="));String item_value = id.substring(id.indexOf("=") + 1);obj.setAttribute(item_key, item_value);}parentElement.addContent(obj);} else {for (int j = 0; j < childList.size(); j++) {if (index1 > 0) {if (childList.size() > Integer.parseInt(itemIndex)) {obj = (Element) childList.get(Integer.parseInt(itemIndex));} else {return false;}} else {obj = (Element) childList.get(j);}if (id.indexOf("=") > 0) {String item_key = id.substring(0, id.indexOf("="));String item_value = id.substring(id.indexOf("=") + 1);if (item_value.equals(obj.getAttributeValue(item_key))) {break;}}}//检查是否有属性值if (id.indexOf("=") > 0) {String item_key = id.substring(0, id.indexOf("="));String item_value = id.substring(id.indexOf("=") + 1);String ss = obj.getAttributeValue(item_key);if(null==obj.getAttributeValue(item_key)||!item_value.equals(obj.getAttributeValue(item_key))){obj = new Element(items[i]);obj.setAttribute(item_key,item_value);parentElement.addContent(obj);}}}/* * 如果循环到了树叶结点,则需要给它赋值 如果是树枝结点,则把当前节点 */if (i == items.length - 1) {obj.addContent(value);}parentElement = obj;}return true;} /** * 返回xml整棵树的字符串,供其他应用调用。 * @return */public static String printXmlString(Document myDocument) {String res = "";XMLOutputter outp = new XMLOutputter();res = outp.outputString(myDocument);System.out.println(res);return res;}}
public class JmsUtils {private static Document doc;private static Element rootElement;public static String parseDataToDraft() {init();XmlUtil.xSetValue(doc, "/sys-header/field<name=Message_Code>", "9004");XmlUtil.xSetValue(doc, "/app-header/field<name=SrcSystem>", "UAAP");XmlUtil.xSetValue(doc, "/body/array/struct/field<name=userName>", "1");XmlUtil.xSetValue(doc, "/body/array/struct/field<name=userAccCode>", "3");XmlUtil.xSetValue(doc, "/body/array/struct/field<name=deptId>", "4");XmlUtil.xSetValue(doc, "/body/array/struct/field<name=systemId>", "dee2e65d6cd848f4a9b8fbfc76a725b6");XmlUtil.xSetValue(doc, "/body/array/struct/field<name=loginTime>", "5");XmlUtil.xSetValue(doc, "/body/array/struct/field<name=cancelTimen>", "6");XmlUtil.xSetValue(doc, "/body/array/struct/field<name=meno>", "7");return XmlUtil.printXmlString(doc);}public static void init(){rootElement = new Element("service");rootElement.setAttribute("name", "PowerModel");doc = new Document(rootElement);}public static void main(String args[]) throws Exception{parseDataToDraft();}}