0

I want to store the values contained in an XML file to a java String Object. Here is my XML code :

<properties>
  <comment>test.xml</comment>
  <entry key="1">test1</entry>
  <entry key="2">test1,test2,test3</entry>
  <entry key="3">test1,test2,test3</entry>
<properties>

I want 'values' to be stored in a string. Eg:

String msg;
if(msg.equals("1")){
  String str1 = //get values of key "1" (ie) test1
}
else if(msg.equals("2")){
  String str2 = //get values of key "2" (ie) test1,test2,test3
}

Is there any way to do it using HashMap or anything?? Thanks.

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
sree127
  • 421
  • 3
  • 9
  • 25

3 Answers3

0

You can use XPATH for it, for instance, it will look like this:

//properties/entry[@key ='1']/text()

Alexander Bezrodniy
  • 8,474
  • 7
  • 22
  • 24
0

This looks like a Java XML properties file. If so, you can load it straight into a Properties object by calling properties.loadFromXML.

Properties prop = new Properties();
FileInputStream fis = new FileInputStream("props.xml");
prop.loadFromXML(fis);
System.out.println("Value of key 1 is: " + prop.getProperty("1")); //prints test1

Note that for this to work, The XML document must have the following DOCTYPE declaration:

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
dogbane
  • 266,786
  • 75
  • 396
  • 414
0

The following link will give you the smallest of xml parsing code to perform your task, XML parsing in Java via Groovy

Community
  • 1
  • 1
Arham
  • 2,072
  • 2
  • 18
  • 30