-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?

3
  • 2
    Show us what "literally most things" have you tried, and comment for each on why it didn't work for you. Commented Dec 15, 2016 at 7:39
  • Tried something like this stackoverflow.com/questions/11833059/… I'm not really able to identify how to see "element" then check the "taxe" value linked to it.... Commented Dec 15, 2016 at 7: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"... Commented Dec 15, 2016 at 7:47

1 Answer 1

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)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.