How to parse xml with child elements with the same name as parent

Viewed 2082

I have a xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<workflow>
    <call name="api1">
        <repeat>100</repeat>
        <delay>60</delay>
        <call name="apicallafterapi1">
            <fields>c_id</fields>
            <repeat>10</repeat>
            <delay>2</delay>
        </call>    
    </call>

    <call name="api2">
        <repeat>1000</repeat>
        <delay>5</delay>
    </call>
    <call name="api3">
        <repeat>1000</repeat>
    </call>
</workflow>  

There can exist call complex elements inside another call element like api1. Is this xml structure valid? If so how can I parse this xml using SAX

class Call {
    String name;
    int repeat;
    int delay;
    List<Call> onResponseCall = new ArrayList<>();

    public void setName(String name) {
        this.name = name;
    }
    public void setRepeat(int repeat) {
        this.repeat = repeat;
    }
    public void setDelay(int delay) {
        this.delay = delay;
    }
    public void addCall(Call c) {
        onResponseCall.add(c);
    }

}
class WorkFlow {
    private List<Call> calls = new ArrayList<>();

    public void addCall(Call c) {
        calls.add(c);
    }
}

@Override
public void characters(char[] buffer, int start, int length) {
    temp = new String(buffer, start, length);
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    temp = "";
    if (qName.equalsIgnoreCase("call")) {
        call = new Call();
        call.setName(attributes.getValue("name"));
    }
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.equalsIgnoreCase("call")) {
        // add it to the list
        workflow.add(call);

    } else if (qName.equalsIgnoreCase("repeat")) {
        call.setRepeat(Integer.parseInt(temp));
    } else if (qName.equalsIgnoreCase("delay")) {
        call.setDelay(Integer.parseInt(temp));
    } else if (qName.equalsIgnoreCase("call")) {
        Call c = new Call();
    }

}  

Where should I call Workflow.add(call) & Call.add(call)

EDIT

<call>
        <name>send_message</name>
        <repeat>1</repeat>
        <delay>2</delay>
        <useParentFields>
            <field>c_id</field>
            <field>m_id</field>
        </useParentFields>
        <uniqueFields>
            <field type="Long.class">d_id</field>
            <field type="Long.class">a_id</field>
        </uniqueFields>
    </call> 
2 Answers
Related