jdom實體與XML轉換
阿新 • • 發佈:2019-02-10
怕忘了,給自己看的
jdom的工具類
package com.cernet.util; import java.io.ByteArrayInputStream; import java.io.IOException; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class JDomUtils { public JDomUtils() { } public static Element createRoot(String rootName) { Element root = new Element(rootName); return root; } public static Element addChild(Element parent, String childName) { Element child = new Element(childName); parent.addContent(child); return child; } public static void addChild(Element parent, Element childName) { parent.addContent(childName); } public static Element addChild(Element parent, String childName, String childValue) { Element child = new Element(childName); child.setText(childValue != null ? childValue : ""); parent.addContent(child); return child; } public static String root2StringWithoutXMLDeclaration(Element root, String encoding) throws JDOMException, IOException { return root2StringWithoutXMLDeclaration(root, encoding, false); } public static String root2StringWithoutXMLDeclaration(Element root, String encoding, boolean formated) throws JDOMException, IOException { XMLOutputter outputter = new XMLOutputter(); Format newFormat = Format.getPrettyFormat(); newFormat.setEncoding(encoding); newFormat.setIndent(" "); newFormat.setExpandEmptyElements(true); outputter.setFormat(newFormat); if (formated) { String msg = outputter.outputString(root); root = string2Root(msg, encoding); } return outputter.outputString(root); } public static String root2String(Element root, String encoding) throws JDOMException, IOException { return root2String(root, encoding, false); } public static String root2String(Element root, String encoding, boolean formated) throws JDOMException, IOException { XMLOutputter outputter; Document document; outputter = new XMLOutputter(); Format newFormat = Format.getPrettyFormat(); newFormat.setEncoding(encoding); newFormat.setIndent(" "); newFormat.setTextMode(org.jdom.output.Format.TextMode.NORMALIZE); outputter.setFormat(newFormat); if (formated) { String msg = outputter.outputString(root); root = string2Root(msg, encoding); } document = null; if (root.getParent() != null) root.detach(); document = new Document(root); return outputter.outputString(document); } public static Element string2Root(String content, String encoding) throws JDOMException, IOException { Document document = string2Document(content, encoding); return document.getRootElement(); } private static Document string2Document(String content, String encoding) throws JDOMException, IOException { int startPoint = content.indexOf("<?xml"); if (-1 != startPoint) content = content.substring(startPoint); if (-1 == content.indexOf("<?xml")) content = (new StringBuilder("<?xml version=\"1.0\" encoding = \"")).append(encoding).append("\"?>").append(content).toString(); ByteArrayInputStream bais; SAXBuilder builder; bais = new ByteArrayInputStream(content.getBytes(encoding)); builder = new SAXBuilder(); return builder.build(bais); } }
做測試用的實體類
package com.cernet.util; public class Book { private String counts; private String startTime; private String endTime; private String success; private String failure; private String downUrl; //set and get method public String getCounts() { return counts; } public void setCounts(String counts) { this.counts = counts; } public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; } public String getSuccess() { return success; } public void setSuccess(String success) { this.success = success; } public String getFailure() { return failure; } public void setFailure(String failure) { this.failure = failure; } public String getDownUrl() { return downUrl; } public void setDownUrl(String downUrl) { this.downUrl = downUrl; } }
使用JDOM工具類
package com.cernet.util; import java.io.IOException; import org.jdom.Element; import org.jdom.JDOMException; public class UseJDomUtilTest { /** * @param args * @throws IOException * @throws JDOMException */ public static void main(String[] args) throws JDOMException, IOException { //將Java物件裡的值轉為XML中的結點 Book book = new Book(); book.setCounts("20"); book.setDownUrl("http://localhost:8080/aaa.xls"); book.setStartTime("2012-09-09 00:11:11"); book.setEndTime("2012-09-09 00:11:12"); book.setFailure("15"); book.setSuccess("5"); String xml = entityToXml(book); System.out.println(xml); //將xml ->轉化為實體類中的物件 Element responseDataElement = JDomUtils.string2Root(xml, "utf-8"); Element returnMessageElement = responseDataElement.getChild("returnMessage"); Element bookElement = returnMessageElement.getChild("Book"); String counts = bookElement.getChildTextTrim("counts"); String downUrl = bookElement.getChildTextTrim("downUrl"); String startTime = bookElement.getChildTextTrim("startTime"); String endTime = bookElement.getChildTextTrim("endTime"); String success = bookElement.getChildTextTrim("success"); String failure = bookElement.getChildTextTrim("failure"); // Book bookNew = new Book(); bookNew.setCounts(counts); bookNew.setDownUrl(downUrl); bookNew.setStartTime(startTime); bookNew.setEndTime(endTime); bookNew.setSuccess(success); bookNew.setFailure(failure); System.out.println("Counts="+bookNew.getCounts()); System.out.println("DownUrl="+bookNew.getDownUrl()); System.out.println("StartTime="+bookNew.getStartTime()); System.out.println("EndTime="+bookNew.getEndTime()); System.out.println("Success="+bookNew.getSuccess()); System.out.println("Failure="+bookNew.getFailure()); } public static String entityToXml(Book book) throws JDOMException, IOException{ Element responseData = JDomUtils.createRoot("responseData"); JDomUtils.addChild(responseData, "returnCode","00001"); Element returnMessage =JDomUtils.addChild(responseData, "returnMessage"); Element bookElement = JDomUtils.addChild(returnMessage, "Book"); JDomUtils.addChild(bookElement, "counts",book.getCounts()); JDomUtils.addChild(bookElement, "downUrl",book.getDownUrl()); JDomUtils.addChild(bookElement, "startTime",book.getStartTime()); JDomUtils.addChild(bookElement, "endTime",book.getEndTime()); JDomUtils.addChild(bookElement, "success",book.getSuccess()); JDomUtils.addChild(bookElement, "failure",book.getFailure()); String xml = JDomUtils.root2String(responseData, "utf-8"); return xml; } }