[英]Modify XML with DomParser
I have a XML file like this: 我有一个像这样的XML文件:
<list>
<job>
<id>B001</id>
<name>前処理</name>
<time>7</time>
<status>success</status>
</job>
<job>
<id>B002</id>
<name>商品データ・商品分類関連データDB取込</name>
<time>1</time>
<status>success</status>
</job>
I'm trying to use DomParser to change time of job whose id = B002 to 3. Here my code: 我正在尝试使用DomParser将ID = B002的工作时间更改为3。这是我的代码:
public boolean changeTimeOfJob(String id, String time) {
try {
ClassLoader classLoader = getClass().getClassLoader();
File inputFile = new File(classLoader.getResource("batch/" + "jobManagement.xml").getFile());
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(inputFile);
Node jobs = doc.getFirstChild();
NodeList childNodes = jobs.getChildNodes();
for (int count = 0; count < childNodes.getLength(); count++) {
Node job = childNodes.item(count);
System.out.println(job.toString());
if (job.getFirstChild().getTextContent().equals(id)) {
NodeList list = job.getChildNodes();
for (int temp = 0; temp < list.getLength(); temp++) {
Node node = list.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
if (eElement.getNodeName().equals("time")) {
eElement.setTextContent(time);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
I got an NullPointerException at this line job.getFirstChild().getTextContent().equals(id)
but I dont know why? 我在此行job.getFirstChild().getTextContent().equals(id)
收到NullPointerException,但我不知道为什么? Anyone know a better way? 有人知道更好的方法吗?
Now Im trying with this code : 现在我正在尝试此代码:
Element docEle = doc.getDocumentElement();
NodeList nl = docEle.getChildNodes();
if (nl != null) {
int length = nl.getLength();
for (int i = 0; i < length; i++) {
if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element el = (Element) nl.item(i);
if (el.getElementsByTagName("id").item(0).getTextContent().equals(id)) {
el.getElementsByTagName("time").item(0).setTextContent(time);
System.out.println(el.getElementsByTagName("id").item(0).getTextContent());
System.out.println(el.getElementsByTagName("time").item(0).getTextContent());
}
}
}
}
But the setTextContent() method did not work 但是setTextContent()方法不起作用
Use XPath: 使用XPath:
String id = "B002";
String time = "3";
XPath xp = XPathFactory.newInstance().newXPath();
String expr = String.format("/list/job[id = '%s']/time/text()", id);
Text textNode = (Text) xp.evaluate(expr, doc, XPathConstants.NODE);
textNode.setNodeValue(time);
UPDATE : Complete example, adapt loading and saving the documnet to your needs. 更新 :完整的示例,根据您的需要调整加载并保存documnet。
public class Main {
static String XML = "<list>\n" +
"<job>\n" +
" <id>B001</id>\n" +
" <name>前処理</name>\n" +
" <time>7</time>\n" +
" <status>success</status>\n" +
"</job>\n" +
"<job>\n" +
" <id>B002</id>\n" +
" <name>商品データ・商品分類関連データDB取込</name>\n" +
" <time>1</time>\n" +
" <status>success</status>\n" +
"</job>\n" + "</list>";
static Document readDocument() throws Exception {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setNamespaceAware(true);
Document doc = f.newDocumentBuilder()
.parse(new InputSource(new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8))));
return doc;
}
static void changeTimeOfJob(Document doc, String id, String time) throws Exception {
XPath xp = XPathFactory.newInstance().newXPath();
String expr = String.format("/list/job[id = '%s']/time/text()", id);
Text textNode = (Text) xp.evaluate(expr, doc, XPathConstants.NODE);
textNode.setNodeValue(time);
}
static void saveDocuemnt(Document doc, OutputStream out) throws Exception {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setNamespaceAware(true);
DOMImplementationLS impl = (DOMImplementationLS) f.newDocumentBuilder().getDOMImplementation();
LSOutput output = impl.createLSOutput();
output.setByteStream(out);
LSSerializer ser = impl.createLSSerializer();
ser.write(doc, output);
}
public static void main(String[] args) throws Exception {
Document doc = readDocument();
changeTimeOfJob(doc, "B002", "3");
saveDocuemnt(doc, System.out);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.