-3

Possible Duplicate:
What Java XML library do you recommend (to replace dom4j)?

I have been googeling for some days now and can't seem to find the right answer to my challenge.

This is what the items in my xml file looks like.

<Item ItemNo="319097" Name="PCB SIO_1" Value="" Config="1" /> 

I would like to take every single part of this item line and break down into strings like this:

 String ItemNo = "319097";

So that I can create a string array that consists of the values in side the "" in the item node of the XML file.

Is this possible?

In advance thanks for helping me.

Community
  • 1
  • 1
KarlNorway
  • 55
  • 1
  • 1
  • 8
  • You can use the JDOM library to parse the document. There are lots of code snippets for that in the internet. – juergen d Dec 16 '11 at 11:04
  • Possible [duplication](http://stackoverflow.com/questions/7153422/java-xml-parser) – Artem Dec 16 '11 at 11:04
  • 1
    Presonally, I would use XSLT for that, not Java, but that is only my opinion. – Miki Dec 16 '11 at 11:04
  • http://jaxb.java.net/tutorial/ – BalusC Dec 16 '11 at 11:04
  • 3
    "Googling for days"? Crazy. "Challenge"? I think not. – duffymo Dec 16 '11 at 11:05
  • What did you google?? `XML`? Try `how to parse XML attributes in Java`. Bingo! 25 million tutorials. And you even have about 50 possible ways to do it! – Lukas Eder Dec 16 '11 at 11:07
  • @duffymo Challenge to because I don't know how ;) LukasEder I actually didn't know it is called "parse" but thanks for the tip... – KarlNorway Dec 16 '11 at 11:31
  • Java is not an acronym and should not be set in all caps. Also, `how to read XML attributes in Java` produces a comparable number of relevant results. Or `how to interpret XML with Java`. Or any other phrasing I can conceive of, really. Also, you tagged your `xml-parse` which is in fact a new tag, so I find your claim "I actually didn't know it is called 'parse'" quite dubious. – Karl Knechtel Dec 16 '11 at 11:36

2 Answers2

4

The cleaneast way would be to unmarshall the XML using Jaxb or something like Xstream : you create a Java object that represent your XML and then Jaxb will create an object and populate it with the XML values. An example for your XML would be :

public class Item {
   private String itemNo;
   private String name;
   private String value;
   private String config;
   ... GETTERS / SETTERS
}
Jean-Philippe Briend
  • 3,455
  • 29
  • 41
1

You can use the DOM API (among others) to select all Item elements and then go though the attributes. A nice library for this is Dom4j. You can find some useful code snippets here: http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html

Tudor
  • 61,523
  • 12
  • 102
  • 142