Reputation: 10586
I am trying to get the text content of first node in this XML. First node (Deductible) is an element node, when I get the text content, I get "Calendar Year$1,500$1,500$3,000" instead of just "Calendar Year"
<Item type="Deductible" name="Deductible" id="a">Calendar Year
<Item type="Text" name="Individual" id="b">$1,500</Item>
<Item type="Text" name="Individual Out-of-network" id="id_4">$1,500</Item>
<Item type="Text" name="Family" id="c">$3,000</Item>
<Item type="Text" name="Family Out-of-network:" id="id_5">$3,000</Item>
</Item>
This is what I am trying
dbuilder = dbc.newDocumentBuilder();
Document doc = dbuilder.parse(new InputSource(new StringReader(plan.getProvisions())));
NodeList nl = doc.getElementsByTagName("Item");
for(int i = 0 ; i < nl.getLength(); i++){
if(i == row){
org.w3c.dom.Element e = (org.w3c.dom.Element)nl.item(i);
value = e.getTextContent();
}
}
Upvotes: 1
Views: 13256
Reputation: 28703
If the root tag is <Item type="Deductible" ...
you can do doc.getDocumentElement().getChildNodes().item(0).getNodeValue();
Otherwise you can check the type
attribute value to detect the Item
element in the nodelist got from getElementsByTagName("Item");
.
I would use the Java(1.5) XPath API to select the Item/@type='Deductible'
elements.
Upvotes: 3