Error reading from xml file in java

i have the following xml:

<Triangle>
    <Color>
        <Red>r-0</Red>
        <Green>g-0</Green>
        <Blue>b-255</Blue>
    </Color>
    <FillColor>
        <Red>r-0</Red>
        <Green>g-0</Green>
        <Blue>b-255</Blue>
    </FillColor>
    <Position>
        <X>x-12</X>
        <Y>y-12</Y>
    </Position>
    <properties>
        <Y1>v-13.0</Y1>
        <X1>v-12.0</X1>
        <Y2>v-15.0</Y2>
        <X2>v-14.0</X2>
    </properties>
</Triangle>

and i want to get values from nodes for example:
the node Y1 has the element v-13.0

i used this method:

Map<String, Double> m = new HashMap<String, Double>();
File xmlFile = new File("Data.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.parse(xmlFile);

NodeList list = xmlDoc.getElementsByTagName("properties");

Node node1 = list.item(0);
Element element1 = (Element) node1;
//Y1
String s = element1.getElementsByTagName("Y1").item(0).getTextContent();
String[] temp ;//= s.split("[-]");
m.put("Y1", Double.parseDouble(temp[1]));

//X1
s =element1.getElementsByTagName("X1").item(0).getTextContent();
temp = s.split("[-]");
m.put("X1", Double.parseDouble(temp[1]));

//Y2
s =element1.getElementsByTagName("Y2").item(0).getTextContent();
temp = s.split("[-]");
m.put("Y2", Double.parseDouble(temp[1]));

//X2
s =element1.getElementsByTagName("X2").item(0).getTextContent();
temp = s.split("[-]");
m.put("X2", Double.parseDouble(temp[1]));

when i used this method to get elements of the X1, Y1, X2, Y2 in properties tag it gives me null pointer exception in the line String s = element1.getElementsByTagName("Y1").item(0).getTextContent() but when i used the same method to get elements of X, Y in the position tag it worked.
what is the solution?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s