我正在修改现有配置 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 中添加行。谢谢