parsing xml DOM child nodes with java



I have following structure of xml



<entities>
<entity>
<type>FieldTerminology</type>
<relevance>0.732316</relevance>
<sentiment>
<type>negative</type>
<score>-0.351864</score>
</sentiment>
<count>2</count>
<text>financial crisis</text>
</entity>
<entity>
<type>Company</type>
<relevance>0.496572</relevance>
<sentiment>
<type>neutral</type>
</sentiment>
<count>1</count>
<text>Goldman Sachs</text>
<disambiguated>
<name>Goldman Sachs</name>
<subType>CompanyShareholder</subType>
<website>http://ift.tt/1yMb8zF;
<dbpedia>http://ift.tt/1xOQ0xE;
<freebase>http://ift.tt/1yMb6b7;
<yago>http://ift.tt/1xOQ0xG;
<crunchbase>http://ift.tt/1yMb8zH;
</disambiguated>
</entity>


I am parsing all, only I cant access to child sentiment with this how can i access also "sentiment" in each entity node?



NodeList feeds = docs.getElementsByTagName("entities");
for (int i = 0; i < feeds.getLength(); i++) {
Node mainNode = feeds.item(i);
if (mainNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstElement = (Element) mainNode;
System.out.println("First element " + firstElement.getTagName());
NodeList forumidNameList = firstElement.getElementsByTagName("entity");

for (int j = 0; j < forumidNameList.getLength(); ++j) {
Element value = (Element) forumidNameList.item(j);

NodeList conditionList = value.getElementsByTagName("relevance");
for (int k = 0; k < conditionList.getLength(); ++k) {
Element condition = (Element) conditionList.item(k);
String conditionText = condition.getFirstChild().getNodeValue();
System.out.println("relevance " + conditionText);
}
NodeList conditionList1 = value.getElementsByTagName("type");
for (int k = 0; k < conditionList1.getLength(); ++k) {
Element condition = (Element) conditionList1.item(k);
String conditionText = condition.getFirstChild().getNodeValue();
System.out.println("type " + conditionText);
}
NodeList conditionList2 = value.getElementsByTagName("count");
for (int k = 0; k < conditionList2.getLength(); ++k) {
Element condition = (Element) conditionList2.item(k);
String conditionText = condition.getFirstChild().getNodeValue();
System.out.println("count " + conditionText);
}
NodeList conditionList3 = value.getElementsByTagName("text");
for (int k = 0; k < conditionList3.getLength(); ++k) {
Element condition = (Element) conditionList3.item(k);
String conditionText = condition.getFirstChild().getNodeValue();
System.out.println("text " + conditionText);
}


I need parse list of entities and also subnodes.


No comments:

Post a Comment