gpt4 book ai didi

java - 将新的 XML 节点保存到文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:48:56 25 4
gpt4 key购买 nike

我正在尝试将包含 XMLnodeList 节点保存为一个新文件,这里是获取新 XML 文档的节点列表并拆分为更小的 XML:

public void split(Document inDocument) throws ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
SaveXML savePerJob = new SaveXML();
// Load the input XML document, parse it and return an instance of the
// Document class.
Document document = inDocument;
//all elements
//jobs
NodeList nodes = document.getDocumentElement().getChildNodes();
NodeList jobs = nodes.item(7).getChildNodes();
for(int j =0; j<jobs.getLength(); j++){
Node itm = jobs.item(j);
String itmName = itm.getFirstChild().getNodeName();
String itmID = itm.getFirstChild().getTextContent();
System.out.println("name: " + itmName + " value: " + itmID);
//here I want to save the node as a new .xml file
}
}

输出是一个长列表,如:

name: id value: 9496425

现在我想将节点 itm 另存为新的 .xml 文件,但我没有找到按原样返回节点的函数。谢谢。

最佳答案

您可以将您的节点转换为字符串并将此字符串保存到.xml 文件中。

将节点转换为字符串

下面的方法会将节点转换为 xml 字符串。这是一个仅限 JDK 的解决方案,不需要任何依赖项。

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.StringWriter;

public static String toString(Node node, boolean omitXmlDeclaration, boolean prettyPrint) {
if (node == null) {
throw new IllegalArgumentException("node is null.");
}

try {
// Remove unwanted whitespaces
node.normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//text()[normalize-space()='']");
NodeList nodeList = (NodeList)expr.evaluate(node, XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); ++i) {
Node nd = nodeList.item(i);
nd.getParentNode().removeChild(nd);
}

// Create and setup transformer
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

if (omitXmlDeclaration == true) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}

if (prettyPrint == true) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
}

// Turn the node into a string
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
} catch (TransformerException e) {
throw new RuntimeException(e);
} catch (XPathExpressionException e) {
throw new RuntimeException(e);
}
}

关于java - 将新的 XML 节点保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33935718/

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