Rave
Rave

Reputation: 843

Extracting values from XML file using Java

I the following format:

<Results>
   <Value type="range"> 45-50 </Value>
   <Value type="range"> 65-58 </Value>
   <Value type="range"> 76-80 </Value>
   <Value type="range"> 48-60 </Value>
</Results>

I want to extract get the type value which is range and also the number.

this is my java code snippet,

NodeList mNode = doc.getElementsByTagName("Value");
String elementAttr = nElement.getAttribute("type"); // here i get the attribute to be "range"

but i am not sure how to extract the actual range "45-50".

Upvotes: 2

Views: 751

Answers (2)

Greg
Greg

Reputation: 1719

This is what I use

public static String attributeText(Node node, String namedItem) {
        String retValue = null;
        Node attribute = node.getAttributes().getNamedItem(namedItem);
        if (attribute != null) {
            retValue = attribute.getNodeValue();
        }
        return retValue;
    }

Upvotes: 2

Gereon
Gereon

Reputation: 17882

try

nElement.getTextContent().trim();

Upvotes: 4

Related Questions