2

How to read XML file into two dimensional array using java. I am new to this concept. Please suggest to me any ideas and suggest any websites and examples regarding this question.

My 2-D xml file will look like this:

<Base>
    <Map>
        <Display>0B85</Display>
        <Keys>61</Keys>
    </Map>
    <Map>
        <Display>0B86</Display>
        <Keys>62</Keys>
    </Map>
</Base>

I want to read this xml file into a two-dimensional array. Suppose I have an array xml[10][40]. In this array I want to display as xml[0][0]=character and xml[0][1]=keys using java program. Please suggest any idea.

1
  • 1
    Urgency won't speed up the answer; a clear problem statement will. This question is too broad and vague. What kind of data is in the XML? How should appear in the array? Have you tried anything at all yourself? Show some code if you have. May 15, 2010 at 10:39

2 Answers 2

2

I would recommend reading it into a map. Use the DocumentBuilder API:

DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse( new File(fileName) );

You can then use the DocumentBuilder's methods to grab the data as required, e.g.

document.getElementsByTagName("NameOfTag");
0

I worked on this and managed to do it here, in my GitHub repo. Basically, I use XStream API to create a DataProvider for TestNG tests. It reads data into a 2-D object array.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.