1

我正在修改现有配置 XML 文件的一些节点。它包含<!DOCTYPE>行。

使用 Xquery 表达式更改 XML 文件后,生成的 XML 文件包含除 <!DOCTYPE>行之外的所有数据。

所以,我<!DOCTYPE>也想要生成的 XML 文件中的行。

我的源 XML 文件:带<!DOCTYPE>

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="c3p0.autoCommitOnClose">false</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

我生成的 XML 文件:没有<!DOCTYPE>行但我想要它

<?xml version="1.0" encoding="UTF-8" standalone="no"?><hibernate-configuration>
    <session-factory>
        <property name="c3p0.autoCommitOnClose">false</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

我的阅读写作逻辑代码是:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder;
        Document doc = null;
        XPathExpression expr = null;
        Node attributeElement = null;
        try {

            builder = factory.newDocumentBuilder();
            // creating input stream
            doc = builder.parse(file);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();

            //expr = xpath.compile("/sitemesh/mapping/@decorator");
            expr = xpath.compile("/hibernate-configuration/session-factory/property/@name='connection.url'");
            attributeElement = (Node) expr.evaluate(doc, XPathConstants.NODE);
            System.out.println("value:"+attributeElement.getNodeValue());

            //attributeElement.setNodeValue("/WEB-INF/views/decorators/" + themeName);

        } catch (Exception e) {
            System.out.println("e:"+e);
        }


        // writing xml file
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer;
        try {
            transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(file);// creating output
                                                            // stream
            transformer.transform(source, result);
        } catch (Exception e) {
        }

只需更改 XML 文件的某个节点后,生成的文件不包含任何<!DOCTYPE>

请帮助,如何<!doctype>在我生成的 xml 中添加行。谢谢

4

2 回答 2

1

我得到了答案。编辑后写入文件时...

transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd");
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(file);// creating output
                                                            // stream
            transformer.transform(source, result);

只需 setOutPutProperty.... 到任何你想要的

谢谢你

于 2013-08-30T07:31:46.800 回答
0

不要对 DOCTYPE 语句进行硬编码,而是使用字符串变量。它将帮助我们在代码中保持松散耦合。

String doctype="http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd";
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,doctype);
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);// creating output stream                                                    
transformer.transform(source, result);

如果你想要很多这样的文档类型,你也可以在属性文件中维护它。这样以后如果有必要可以很容易地改变。

于 2013-09-02T05:32:45.887 回答