1、创建Document实例的方法代码
public InstanceCreator(InputStream in) {
try {
this.relations = new HashMap<Relation,Set<List<String>>>();
this.atoms = new LinkedHashSet<String>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
this.document = builder.parse(in);
} catch (SAXException sxe) {
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
throw new InstanceCreationException("Error generated during parsing: " + x.getMessage());
} catch (ParserConfigurationException pce) {
throw new InstanceCreationException("Parser with specified options cannot be built: " + pce.getMessage());
} catch (IOException ioe) {
throw new InstanceCreationException("I/O error: " + ioe.getMessage());
} finally {
try {
in.close();
} catch (IOException e) {
}
}
}
2、保存Emitter(ConfigurableEmitter)到xml文件中
public static void saveEmitter(OutputStream out, ConfigurableEmitter emitter)
throws IOException {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = builder.newDocument();
document.appendChild(emitterToElement(document, emitter));
Result result = new StreamResult(new OutputStreamWriter(out,
"utf-8"));
DOMSource source = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer xformer = factory.newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
} catch (Exception e) {
Log.error(e);
throw new IOException("Failed to save emitter");
}
}
3、转成org.w3c.dom.Document
private static org.w3c.dom.Document toDom(final Document doc) {
try {
final XMLOutputter xmlOutputter = new XMLOutputter();
final StringWriter elemStrWriter = new StringWriter();
xmlOutputter.output(doc, elemStrWriter);
final byte[] xmlBytes = elemStrWriter.toString().getBytes();
final DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
dbf.setNamespaceAware(true);
return dbf.newDocumentBuilder().parse(
new ByteArrayInputStream(xmlBytes));
} catch (final Exception e) {
return null;
}
}
4、获取Document对象代码
private static Document getDocument(InputStream in) {
Document document = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(in);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
return document;
}
5、格式化xml文件
private static String formatXml(Context context, String src) throws TransformerException,
ParserConfigurationException, IOException, SAXException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(src)));
AppSetting setting = new AppSetting(context);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", setting.getTab().length() + "");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
}