0

I used a java parser for parsing below xml. But I didn't find a perfect parser or I may don't know how to use it.

<checklist> <checklistitem>
    <checklistId>2</checklistId>
    <labels>
      <label>Label 1</label>
    </labels>
    <parents>
      <parent/>
    </parents>
    <children/>
    <descriptions>
      <short>General Application Checks</short>
      <long>General Application Checks</long>
    </descriptions>
    <displayPrerequisite>Always</displayPrerequisite>
    <effort>Hard</effort>
    <priority>Low</priority>
    <platform></platform>   </checklistitem>   <checklistitem>
    <checklistId>3</checklistId>
    <labels>
      <label>Label 1</label>
    </labels>
    <parents>
      <parent/>
    </parents>
    <children>
      <child>4</child>
      <child>5</child>
      <child>6</child>
    </children>
    <descriptions>
      <short>Uses a certificate with a known key</short>
      <long>Uses a certificate with a known key</long>
    </descriptions>
    <displayPrerequisite>Tool Driven</displayPrerequisite>
    <effort>Hard</effort>
    <priority>Low</priority>
    <platform>ios</platform>   </checklistitem> </checklist>

My problem to return a array list of a class with a java file by parsing this. and the class should look like below.

    public int checklistId;
    public ArrayList<String> labels = new ArrayList<String>();
    public ArrayList<String> parents = new ArrayList<String>();
    public ArrayList<Integer> children = new ArrayList<Integer>();
    public String[] discriptions = new String[2];
    public String displayPrerequisite;
    public String effort;
    public String priority;
    public String platform;
Akhil P
  • 1,580
  • 2
  • 20
  • 29
  • Show us your try...and ask where exactly are you facing problem in parsing. – vish4071 Dec 28 '15 at 10:41
  • @vish4071 I am failing to try even with SAX Parser. I am not getting how to use it for this problem. At lease let me know how to parse it. I can make it into a object. – Akhil P Dec 28 '15 at 10:48
  • That is fine, but without a look at your code, how can we tell where you are going wrong? – vish4071 Dec 28 '15 at 10:49
  • @vish4071 atlease let me know how to parse it. I don't know how to use SAX parser actually. I don't have time to read the documentation now. This is a bit emergency for me. – Akhil P Dec 28 '15 at 10:50

1 Answers1

1

1 You have to parse your XML

see this, for examples:

DOM XML Parser Example

and check xpath too: it is very useful for you problem

2 define a strategy and rules

3 with your example, I propose that:

  • get the n-1 tags: labels, parents, priority

  • if one string value => String

  • if one int value => int

  • if multiple sub-tags with simple type (short, long, why ?) => array of Strings

  • if sub-tags of other types: ArrayList

and code it: good luck !

Community
  • 1
  • 1