0

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

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

首先,我用 .I 拆分内容|。我将获得标题和全名。然后我用空格分割全名以获得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>

如果firstnameand familynamecontets 与上述 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");
                   }
            }
        }

但是我如何让孩子们检查文本内容并在特定位置创建节点?

4

1 回答 1

1
子节点附加:
    元素 childElement = doc.createElement("title");
    // 设置属性等
    node.appendChild(childElement);

可以使用 xPath 简化代码,例如:

    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>
于 2013-11-04T09:34:59.377 回答