kamil.rak
kamil.rak

Reputation: 1378

order a list like in xml schema java

I have an xml like this one:

<?xml version="1.0" encoding="UTF-8"?>
<plane>
<part>
    <desc>qwe</desc>
    <qty>1</qty>
</part>
<part>
    <desc>asd</desc>
    <qty>1</qty>
        <part>
            <desc>asdfg</desc>
            <qty>1</qty>
        </part>
        <part>
            <desc>asdfg</desc>
            <qty>4</qty>
        </part>
    </part>
<part>
    <desc>zxc</desc>
    <qty>2</qty>
</part>
<part>
    <desc>AAAA</desc>
    <qty>1</qty>
        <part>
            <desc>BBBB</desc>
            <qty>1</qty>
                <part>
                    <desc>CCCC</desc>
                    <qty>13</qty>
                        <part>
                            <desc>DDDD</desc>
                            <qty>12</qty>
                                <part>
                                    <desc>EEEE</desc>
                                    <qty>21</qty>
                                </part>
                        </part>
                </part>
                <part>
                    <desc>CCCC1111</desc>
                    <qty>13</qty>
                        <part>
                            <desc>DDDD1111</desc>
                            <qty>12</qty>
                                <part>
                                    <desc>EEEE1111</desc>
                                    <qty>21</qty>
                                </part>
                        </part>
                </part>
        </part>
        <part>
            <desc>G</desc>
            <qty>1</qty>
                <part>
                    <desc>H</desc>
                    <qty>13</qty>
                        <part>
                            <desc>I</desc>
                            <qty>12</qty>
                        </part>
                </part>
        </part>
</part>
</plane>

Now I want to print it out in the "same" structure. I created a method, but the output differs from expectes: Run. class

public class Run {

public static void itarate(List<Part> list){
    for (int i = 0; i<list.size(); i++){
        System.out.println(list.get(i).getDesc() + " x " + list.get(i).getQty());
        if (list.get(i).getListParts() != null){
            System.out.print(" -- ");
            itarate(list.get(i).getListParts());

        }
    }
}


    public static void main(String[] args) {



    Converter converter = new XML_Converter();
    Plane plane = converter.readFile();
    //System.out.println(plane);
    List<Part> parts = plane.getPlaneParts();


    itarate(parts);

}

}

The output:

qwe x 2
AAAA x 1
  BBBB x 1
  CCCC x 13
  DDDD x 12
  EEEE x 21
CCCC1111 x 13
  DDDD1111 x 12
  EEEE1111 x 21
G x 1
  H x 13
  I x 12

but I actually want something like:

qwe x 2
AAAA x 1
  BBBB x 1
    CCCC x 13
      DDDD x 12
        EEEE x 21
  CCCC1111 x 13
    DDDD1111 x 12
      EEEE1111 x 21
G x 1
  H x 13
    I x 12

XML To object Class:

public class XML_Converter{

private String filePath = "D:\\file.xml";
private File file = new File(filePath);

@Override
public Plane readFile() {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Plane.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Plane plane = (Plane) jaxbUnmarshaller.unmarshal(file);
        return plane;
    } catch (JAXBException e) {
        System.out.println("Unable to load file. Try again.");
    }
    return new Plane();

}

Plane class

@XmlRootElement
public class Plane {

private List<Part> planeParts = null;


@XmlElement(name="part")
public List<Part> getPlaneParts() {
    return planeParts;
}

public void setPlaneParts(List<Part> planeParts) {
    this.planeParts = planeParts;
}

@Override
public String toString() {
    return "Plane [planeParts=" + planeParts + "]";
}


}

Parts.class

public class Part {

private String desc;
private int qty;

private List<Part> listParts = null;


@XmlElement(name="desc")
public String getDesc() {
    return desc;
}

@XmlElement(name="part")
public List<Part> getListParts() {
    return listParts;
}

@XmlElement(name="qty")
public int getQty() {
    return qty;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public void setListParts(List<Part> listParts) {
    this.listParts = listParts;
}

public void setQty(int qty) {
    this.qty = qty;
}

@Override
public String toString() {
    return "Part [desc=" + desc + ", qty=" + qty + ", listParts="
            + listParts + "]";
}


}

How can I achieve that (so the xml structure is similar to the code output)? Thanks in advance.

Upvotes: 0

Views: 55

Answers (1)

Oncaphillis
Oncaphillis

Reputation: 1908

Pass the current recursion level around in a variable (e.g r) and print the number of spaces accordingly

public static void itarate(List<Part> list,int r){
    for (int i = 0; i<list.size(); i++){
        for(int j=0;j<r;j++)
           System.out.print("  ");
        System.out.println(list.get(i).getDesc() + " x " + list.get(i).getQty());
        if (list.get(i).getListParts() != null){
            itarate(list.get(i).getListParts(),r+1);

        }
    }

EDIT: Moved the indentation stuff out of the inner loop

Upvotes: 2

Related Questions