-3

I'm trying to create an array list out of an XML file.

In fact, I need to read a user input(which would be one of the elements) and return a certain node value.Here's the XML file:

<?xml version="1.0" ?> 
- <types>
- <type id="Nourriture" taxe="0.1">
  <element>pomme</element> 
  <element>fraise</element> 
  <element>fromage</element> 
  <element>viande rouge</element> 
  </type>
- <type id="Matiere Premiere" taxe="0.2">
  <element>fer</element> 
  <element>polypropylene</element> 
  </type>
- <type id="Element Solide" taxe="0.3">
  <element>voiture</element> 
  <element>planche surf</element> 
  <element>pistolet</element> 
  </type>
  </types>

What I'm asked for is to see element, then depending on that, I need to return the "taxe" value.

I literally tried most things to do that, until I noticed my only solution is putting them inside an array /array list as follows:

`ArrayList  arraylistobject = new ArrayList();
 arraylistobject.add(....);` 

etc... Any thoughts on how to do this?

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
Grizzly
  • 3
  • 1
  • 4
  • 2
    Show us what "literally most things" have you tried, and comment for each on why it didn't work for you. – Jiri Tousek Dec 15 '16 at 07:39
  • Tried something like this http://stackoverflow.com/questions/11833059/convert-xml-string-to-arraylist I'm not really able to identify how to see "element" then check the "taxe" value linked to it.... – Grizzly Dec 15 '16 at 07:46
  • But the fact is, how can I arraylist.add a value from the xml file..... Like first case would be the value on taxe= 0.1 and element "pomme"... – Grizzly Dec 15 '16 at 07:47

1 Answers1

0

One obvious way is to use XPath:

//element[text()="fer"]/parent::type/string(@taxe)

(search for element whose text is "fer", select it's parent type, get string value of the parent's taxe attribute)

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43