如何使用Java中的DOM解析器在XML文件中声明doctype,xml版本和编码? [英] How to declare doctype ,xml version and encoding in XML file using DOM parser in java?

查看:324
本文介绍了如何使用Java中的DOM解析器在XML文件中声明doctype,xml版本和编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我不知道该怎么做。

For sorry, I don't know how to attempt to do such thing.

public void writeXML(String tableName) {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement(tableName);
        doc.appendChild(rootElement);

        for(Map m: rows){
            Element parent = doc.createElement((String) m.get("ownerNode"));
            rootElement.appendChild(parent);
            for(String s: colNames){
                String key = (String) m.get(s);
                System.out.println(key);
                Element innerNode = doc.createElement(s);
                innerNode.appendChild(doc.createTextNode((String) m.get(s)));
                parent.appendChild(innerNode);
            }
        }



        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(tableName + ".xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("File saved!");

    } catch (ParserConfigurationException pce) {
    } catch (TransformerException tfe) {
    }

}

这是我编写xml正文的代码,因此如果有人可以帮助我添加XML版本,编码,doctype和带有DOM解析器的DTD参考

this is my code to write the body of the xml so it would be greatly appreciated if can anybody help me to add XML version,encoding, doctype and DTD reference with DOM parser

推荐答案

代码显示了默认行为以及更改方式:

Code showing the default behavior and the way to change that:

String inputXml = "<root><value>Test</value></root>";

// Build DOM tree for input XML
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document document = domBuilder.parse(new InputSource(new StringReader(inputXml)));

// Print DOM XML using default settings
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(System.out));

System.out.println();   System.out.println();

// Print DOM XML using specific settings
document.setXmlVersion("1.1");
transformer.setOutputProperty(OutputKeys.ENCODING, "windows-1252");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "ROOT-VALUE");
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "RootValue.dtd");
transformer.transform(new DOMSource(document), new StreamResult(System.out));

Output (使用Java 1.8.0_51)

Output (using Java 1.8.0_51)

<?xml version="1.0" encoding="UTF-8" standalone="no"?><root><value>Test</value></root>

<?xml version="1.1" encoding="windows-1252" standalone="no"?><!DOCTYPE root PUBLIC "ROOT-VALUE" "RootValue.dtd">
<root><value>Test</value></root>

这篇关于如何使用Java中的DOM解析器在XML文件中声明doctype,xml版本和编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆