Tôi đang sửa đổi một số nút của tệp XML cấu hình hiện có. nó chứa
Dây điện.
Sau khi thay đổi tệp XML bằng cách sử dụng biểu thức Xquery, tệp XML kết quả chứa
Tất cả dữ liệu ngoại trừ . Dây điện.
Vì vậy, tôi muốn
Dòng này cũng được bao gồm trong tệp XML được tạo.
Tệp XML nguồn của tôi: với
Dây điện
false
org.hibernate.transaction.JDBCTransactionFactory
org.hibernate.dialect.MySQLDialect
Tệp XML tôi đã tạo: Không có
dòng nhưng tôi muốn nó
false
org.hibernate.transaction.JDBCTransactionFactory
org.hibernate.dialect.MySQLDialect
Mã logic đọc và viết của tôi là:
Nhà máy DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
Trình tạo DocumentBuilder;
Tài liệu doc = null;
XPathExpression expr = null;
Thuộc tính nútElement = null;
thử {
builder = Factory.newDocumentBuilder();
// tạo luồng đầu vào
doc = builder.parse(file);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
//expr = xpath.compile("/sitemesh/mapping/@designator");
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/trang trí/" + themeName);
} catch (Exception e) {
System.out.println("e:"+e);
}
// ghi file xml
TransformerFactory máy biến ápFactory = TransformerFactory
.newInstance();
Máy biến áp;
thử {
máy biến áp = máy biến ápFactory.newTransformer();
Nguồn DOMSource = DOMSource mới (doc);
Kết quả StreamResult = new StreamResult(file); // tạo đầu ra
// suối
Transform.transform(nguồn, kết quả);
} catch (Exception e) {
}
Sau khi chỉ thay đổi một số nút của tệp XML, tệp kết quả không chứa bất kỳ nút nào
Dây điện
Xin hãy giúp đỡ, làm thế nào để thêm
Một hàng trong xml tôi đã tạo. Cảm ơn
Tôi đã có câu trả lời của mình. Khi ghi vào một tập tin sau khi chỉnh sửa...
máy biến áp = máy biến ápFactory.newTransformer();
biến áp.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd");
Nguồn DOMSource = DOMSource mới (doc);
Kết quả StreamResult = new StreamResult(file); // tạo đầu ra
// suối
Transform.transform(nguồn, kết quả);
Chỉ cần thay đổi OutPutProperty....Đặt thành bất cứ điều gì bạn muốn
Cảm ơn
Tôi là một lập trình viên xuất sắc, rất giỏi!