首页

关于fedora-support源码包DocumentUtils的XML文件工具类字符串、二进制数组和XML文件相互转换的处理操作

标签:DocumentUtils,fedora-support,文档工具类,xml解析     发布时间:2018-04-28   

一、前言

关于fedora-support源码包中eu.planets_project.ifr.core.storage.impl.fedora.connector.DocumentUtils的xml文档工具类,实现xml文档转为字符串documentToString、字符串转为xml文档stringToDocument、字符串数据转XML文档等处理。

二、源码说明

package eu.planets_project.ifr.core.storage.impl.fedora.connector;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.IOException;@b@import java.io.StringWriter;@b@import javax.xml.parsers.DocumentBuilder;@b@import javax.xml.parsers.DocumentBuilderFactory;@b@import javax.xml.parsers.ParserConfigurationException;@b@import javax.xml.transform.Transformer;@b@import javax.xml.transform.TransformerConfigurationException;@b@import javax.xml.transform.TransformerException;@b@import javax.xml.transform.TransformerFactory;@b@import javax.xml.transform.dom.DOMSource;@b@import javax.xml.transform.stream.StreamResult;@b@import org.w3c.dom.Document;@b@import org.xml.sax.SAXException;@b@@b@public class DocumentUtils@b@{@b@  public static final DocumentBuilder DOCUMENT_BUILDER;@b@  public static final Transformer DOCUMENT_TRANSFORMER;@b@@b@  public static String documentToString(Document paramDocument)@b@    throws TransformerException@b@  {@b@    StringWriter localStringWriter = new StringWriter();@b@    DOCUMENT_TRANSFORMER.transform(new DOMSource(paramDocument), new StreamResult(localStringWriter));@b@    return localStringWriter.toString();@b@  }@b@@b@  public static Document stringToDocument(String paramString)@b@    throws SAXException@b@  {@b@    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramString.getBytes());@b@    try@b@    {@b@      Document localDocument = DOCUMENT_BUILDER.parse(localByteArrayInputStream);@b@      return localDocument;@b@    }@b@    catch (IOException localIOException)@b@    {@b@      throw new Error("Problem reading a string, should never happen", localIOException);@b@    }@b@  }@b@@b@  public static Document bytesToDocument(byte[] paramArrayOfByte)@b@    throws SAXException@b@  {@b@    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);@b@    try@b@    {@b@      return DOCUMENT_BUILDER.parse(localByteArrayInputStream);@b@    }@b@    catch (IOException localIOException)@b@    {@b@      throw new Error("Problem reading a string, should never happen", localIOException);@b@    }@b@  }@b@@b@  static@b@  {@b@    DocumentBuilderFactory localDocumentBuilderFactory;@b@    try@b@    {@b@      localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();@b@      localDocumentBuilderFactory.setNamespaceAware(true);@b@      DOCUMENT_BUILDER = localDocumentBuilderFactory.newDocumentBuilder();@b@    }@b@    catch (ParserConfigurationException localParserConfigurationException)@b@    {@b@      throw new Error("Error initialising default document builder", localParserConfigurationException);@b@    }@b@    try@b@    {@b@      DOCUMENT_TRANSFORMER = TransformerFactory.newInstance().newTransformer();@b@      DOCUMENT_TRANSFORMER.setOutputProperty("omit-xml-declaration", "yes");@b@    }@b@    catch (TransformerConfigurationException localTransformerConfigurationException)@b@    {@b@      throw new Error("Error initialising default document transformer", localTransformerConfigurationException);@b@    }@b@  }@b@}