JDOM common conversion method

zhaozj2021-02-16  42

import java.io *;. import javax.xml.transform *;. import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.jdom *;. import org.jdom. INPUT. *; Import Org.jdom.output. *;

/ ** * This class provides some method commonly used by JDM objects. * @Author Liu Hailong (xiaoleilong@mail.biti.edu.cn) * / public class jdomutil {

/ ** * Establish a JDOM object * @Param filepath XML file based on the XML file of the specified path * @return Returns the established JDOM object, and establishing unsuccessful returns NULL. * / Public static Document buildFromFile (String filePath) {try {SAXBuilder builder = new SAXBuilder (); Document anotherDocument = builder.build (new File (filePath)); return anotherDocument;} catch (JDOMException e) {e.printStackTrace () } Catch (nullpointerException e) {E.PrintStackTrace ();} return null;} / ** * established a JDOM object * @Param Xmlstring XML format * @return returned to the established JDM object according to the XML string Successfully returned NULL. * / Public static Document buildFromXMLString (String xmlString) {try {SAXBuilder builder = new SAXBuilder (); Document anotherDocument = builder.build (new StringReader (xmlString)); return anotherDocument;} catch (JDOMException e) {e.printStackTrace () } CatCH (NullPointerException E) {E.PrintStackTrace ();} return null;} / ** * Established a JDOM object according to the DOM object * @Param Dom org.w3c.dom.Document object * @return returns the established JDOM object, Establishing unsuccessful returns NULL. * / Public static Document buildFromDom (org.w3c.dom.Document Dom) throws JDOMException, IOException {org.jdom.input.DOMBuilder builder = new org.jdom.input.DOMBuilder (); Document jdomDoc = builder.build (Dom) Return JDOMDOC;

/ ** * This method uses XMLOUTPUTER to output a JDOM object to a standard output device, using GBK code * @Param MyDocument will be output JDOM object * / public static void outputTostDout (Document MyDocument) {OutputTostDOut (MyDocument, "GBK") } / ** * This method uses XMLOUTPUTER to output a JDOM object to a standard output device * @Param myDocument will be output Jdom object * @Param encoding output Used code * / public static void OutputTostDout (Document MyDocument, String Encoding) {Try {xmloutputter outputter = new xmloutputter (", true, eNCoding); outputter.output (myDocument, system.out);} catch (java.io.ioException e) {E.PrintStackTrace ();}} / ** * This method converts the JDOM object to the string. * @Param Document will be converted to the JDOM object * / public static string OutputtoTRING (Document Document) {Return OutputTostring (Document, "GBK");} / ** * This method will jdom Object conversion string. * @Param Document will be converted to the JDOM object * @Param Encoding Output String * / Public Static String OutputTostring (Document Document, String Encoding) {bytearrayoutputstream byteRep = new ByteArrayOutputStream (); XMLOutputter docWriter = new XMLOutputter ( "", true, encoding); try {docWriter.output (document, byteRep);} catch (Exception e) {} return byteRep.toString ();} public static org.w3c.dom.Document outputToDom (org.jdom.Document jdomDoc) throws JDOMException {org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter (); return outputter.output (jdomDoc);} / * * * This method uses XMLOUTPUTTER to output a JDOM object to the file * @Param myDocument will output the JDOM object * @

param filePath to be output to the disk path * / public static void outputToFile (Document myDocument, String filePath) {outputToFile (myDocument, filePath, "GBK");} / ** * This method uses XMLOutputter output to a file JDom Object * @ the object to be JDom param myDocument * @param filePath output to be output to the disk path * @param encoding encoding * / public static void outputToFile (Document myDocument, String filePath, String encoding) {// setup this like outputDocument try {XMLOutputter outputter = New XMLOUTPUTTER ("True, Encoding; // Output to a file filewriter Writer = New FileWriter (filepath); Outputter.Output (MyDocument, Writer); Writer.close ();

} catch (java.io ioException e) {E.PrintStackTrace ();}} / ** * This method will convert the JDOM object through style single conversion. * @Param MyDocument will be converted to the JDOM object * @Param xslfilepath XSL file disk path * / public static void executeXSL (Document myDocument, String xslFilePath, StreamResult xmlResult) {try {TransformerFactory tFactory = TransformerFactory.newInstance (); // Make the input sources for the XML and XSLT documents org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter (); org.w3c.dom.Document domDocument = outputter.output (myDocument); javax.xml.transform.Source xmlSource = new javax.xml.transform.dom.DOMSource (domDocument) ; StreamSource xsltSource = new StreamSource (new FileInputStream (xslFilePath)); // Get a XSLT transformer Transformer transformer = tFactory.newTransformer (xsltSource); // do the transform transformer.transform (xmlSource, xmlResult);} catch (FileNotFoundException e) { e.printStackTrace ();} catch (TransformerConfigurationException e) {e.printStackTrace ();} catch (TransformerException e) {e.printStackTrace ();} catch (org.jdom.JDOMException e) {e.printStackTrace ();} } // main function, local test. Public static void main (String Argv []) {

}

转载请注明原文地址:https://www.9cbs.com/read-25755.html

New Post(0)