gpt4 book ai didi

java - Java中比较xml元素的文本内容并将子元素添加到特定节点

转载 作者:行者123 更新时间:2023-12-01 13:53:39 26 4
gpt4 key购买 nike

我有一个数组列表,其中包含以下格式的标题和全名

Dr.|ricardo bettati
Prof. Dr.-Ing.|Helmut sperling
miss|angela link

首先,我将内容与 | 分开.我会得到头衔和全名。然后我用空格分割全名以获得 firstnamefamilyname.

现在我有 3 个字符串

字符串标题\其中包含上面列表中的标题字符串名字;字符串姓氏;

下一个任务是我使用上述字符串检查现有 xml 的文本内容 firstnamefamilyname .这是现有的xml

<person>
<name>
<firstname>Jean-Luc</firstname>
<familyname>DeLorme</familyname>
</name>
</person>
<person>
<name>
<firstname>Dave</firstname>
<familyname>Sperling</familyname>
</name>
</person>
<person>
<name>
<firstname>Helmut</firstname>
<familyname>sperling</familyname>
</name>
</person>

如果firstnamefamilyname contets 与上述 xml 元素 <firstname> 的文本内容匹配, <familyname>然后我必须创建一个名为 <title> 的元素并从我的列表中附加相应的标题。我的 XML 应该如下所示

<person>
<name>
<firstname>Jean-Luc</firstname>
<familyname>DeLorme</familyname>
</name>
</person>
<person>
<name>
<firstname>Dave</firstname>
<familyname>Sperling</familyname>
</name>
</person>
<person>
<name>
**<title>Prof. Dr.-Ing.>/title>**
<firstname>Helmut</firstname>
<familyname>sperling</familyname>
</name>
</person>

我尝试过以下代码

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(new File("Final-Results.xml")));
Element element = doc.getDocumentElement();
NodeList list = doc.getElementsByTagName("person");
for (int i=0;i<titles.size();i++) { //Title is my arraylist
String names = (String) titles.get(i);
String[] content=names.split("\\|");
String title=content[0];
String[] fullname=content[1].split("\\s+");
String firstname=fullname[0];
String familyname=fullname[1];
for (int n = 0; n < list.getLength(); n++) {

Node node = list.item(n);
if ("firstname".equals(node.getNodeName()) && node.getTextContent().equals(firstname)) {
System.out.println("haii");
}
}
}

但是我如何获取子项并检查文本内容并在特定位置创建节点?

最佳答案

Child node appending:    Element childElement = doc.createElement("title");    // set attributes, etc    node.appendChild(childElement);

Code can be simplified with xPath, like:

    String xPathExpression = "*/person/name[firstname/text() = 'Helmut' and familyname/text() = 'sperling']";
XPath path = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) path.evaluate(xPathExpression, doc, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
Element childElement = doc.createElement("title");
childElement.appendChild(doc.createTextNode("Dr."));
nl.item(i).insertBefore(childElement,((Element)nl.item(i)).getFirstChild());
}

如果在指定示例上运行代码,结果为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><result>
<person>
<name>
<firstname>Jean-Luc</firstname>
<familyname>DeLorme</familyname>
</name>
</person>
<person>
<name>
<firstname>Dave</firstname>
<familyname>Sperling</familyname>
</name>
</person>
<person>
<name><title>Dr.</title>
<firstname>Helmut</firstname>
<familyname>sperling</familyname>
</name>
</person>
</result>

关于java - Java中比较xml元素的文本内容并将子元素添加到特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19764546/

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