Reputation: 57
i have an arraylist which contains title and fullname in the following format
Dr.|ricardo bettati
Prof. Dr.-Ing.|Helmut sperling
miss|angela link
First i am splitting the contents with |
.I will get the title and fullnames. Then i split fullnames with space to get firstname
and familyname.
Now i have 3 strings
String title\which contains title from above list String firstname; String familynames;
The next task is i have check the text contents of existed xml with the above strings firstname
and familyname
.Here is the existed 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>
If the firstname
and familyname
contets matches with the text content of above xml elements <firstname>
,<familyname>
then i have to create an element named <title>
and append the corresponding title from my list.My XML supposed to look like the following
<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>
I have tried with the following code
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");
}
}
}
But how do i get the children and check the text contents and create node at particular place?
Upvotes: 0
Views: 221
Reputation: 7207
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());
}
If run code on specified example, result is:
<?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>
Upvotes: 1