我有一个像这样的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>
我试图使用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;
}
我在这一行job.getFirstChild().getTextContent().equals(id)
得到了一个NullPointerException,但我不知道为什么?谁知道更好的方法?
现在我正在尝试使用此代码:
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());
}
}
}
}
但是setTextContent()方法不起作用
答案 0 :(得分:0)
使用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);
更新:完成示例,根据需要调整加载并保存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);
}
}