gpt4 book ai didi

java - Xpath Xquery : unable to add 在 xml 中,而它存在于源 xml 文件中

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:15 26 4
gpt4 key购买 nike

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

最佳答案

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

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);

只需将OutPutProperty....设置为您想要的任何内容

谢谢

关于java - Xpath Xquery : unable to add <DOCTYPE! > 在 xml 中,而它存在于源 xml 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15408941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com