1

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?

7
  • 1
    Works fine for me. Have you doube-checked that Y1 is present in your input XML? Commented Oct 29, 2015 at 11:07
  • 1
    Copied it exactly as above and run it, no exceptions, output of 'm' = "{X1=12.0, X2=14.0, Y1=13.0, Y2=15.0}" I can only assume you are pointing at a dodgy xml file. Commented Oct 29, 2015 at 11:08
  • @TomMac yes i checked Commented Oct 29, 2015 at 11:23
  • @RossDrew what do you think is the problem? Commented Oct 29, 2015 at 11:25
  • 1
    The only thing I could see as a problem is if the xml file is wrong. As I said, the above works fine for me and everyone else. Commented Oct 29, 2015 at 11:46

3 Answers 3

1

Works fine on my computer.

map : {Y1=13.0, X1=12.0, Y2=15.0, X2=14.0}

Is it possible that you are testing with the wrong xml file ? Hint: try specifying the full absolute system path to your "data.xml" file. You may find out that java used a different file.

PS: not really relevant, but it's better to use Double.valueOf(String) to convert the strings. The parseDouble method results in a primitive double, which requires java to perform auto-boxing. The valueOf on the other hand immediately creates a wrapper Double object (no auto-boxing necessary).

Another thing to check: xml tags are case sensitive. According to your java code, your <properties> tag has to be in lower-case. Your code will not work if your xml file contains a <Properties> node instead.

Sign up to request clarification or add additional context in comments.

2 Comments

@Mark Mamdouh and you are testing with the exact same file ? or is the content different ? (xml is case sensitive, so that could also be it) The <properties> tag has to be in lower-case.
i got the problem, i have <properties> tag for all shapes i have that's why it gives me error , thank you @bvdb
0

i change String[] temp ;//= s.split("[-]");

to String[] temp = s.split("[-]");

and work fine with me

4 Comments

indeed, that's also the thing I changed to make it running. So, in my opinion there's nothing wrong with the java code.
@Nemaky i have your code in my code it is a mistake in the post, i edited it
can you write the exception here and check the list length
i got the problem, i have <properties> tag for all shapes i have that's why it gives me error , thank you @Nemaky
0

i have many shapes in my XML file and every shape has properties tag that's why it gives me error

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.