...">

How to parse xml with children with the same name as parent

I have xml:

<?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>  

      

In another element call

such as complex elements api1

may exist call

. Is this xml structure valid? If so, how can I parse this xml usingSAX

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 to call me Workflow.add(call)

andCall.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> 

      

+3


source to share


2 answers


I am wondering how to do this and the solution seems to be quite simple. To play with the main solution, you can check commit .

If you only want the basic answer, check the code below:

Workflow

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "workflow")
public class Workflow {
    @XmlElement(name="call")
    private List<Call> calls;
}

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

      

Call

@XmlAccessorType(XmlAccessType.FIELD)
public class Call {

    @XmlAttribute
    private String name;
    private String repeat;
    private String delay;
    private String fields;
    @XmlElement(name="call")
    private List<Call> call;

}

      

enter a dot like



import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Main {
    public static void main(String[] args) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Workflow.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/variant.xml");
        Workflow sc = (Workflow) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "Workflow.xml");
        marshaller.marshal(sc, System.out);

    }
}

      

variant.xml - your xml

<?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>

      

Hope this is clear with a general example, but ask if there are any questions.

You can handle the comparison of names if I already parsed the xml.


To make objects more useful you can add getter \ setter \ equals \ hashCode etc.

+1


source


I solved this by taking the following approach. Modify the xml to define the parent and child nodes.

workflow.xml

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

    <call type="parent">
        <name>api2</name>
        <repeat>1000</repeat>
        <delay>5</delay>
    </call>
    <call type="parent">
        <name>api3</name>
        <repeat>1000</repeat>
    </call>
</flow>

      



Parse code

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    temp = "";
    if (qName.equalsIgnoreCase("call")) {
        if(attributes.getValue("type").equals("parent")) {
            flow.isParent = true;
            parentCall = new Call(); //parent call
            parentCall.setType(attributes.getValue("type"));
        }
        else {
            flow.isParent = false;
            childCall = new Call(); //Child call
            childCall.setType(attributes.getValue("type"));
        }
    }
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    Call c = flow.isParent ? parentCall : childCall;
    if (qName.equalsIgnoreCase("call")) {
        // add it to the list
        if(flow.isParent) { //add to workflow 
            flow.addCall(parentCall);
        }
        else {
            parentCall.onResponseCall.add(childCall);
            flow.isParent = true;
        }
    }
    else if (qName.equalsIgnoreCase("name")) {
        c.setName(temp);
    }
    else if (qName.equalsIgnoreCase("repeat")) {
        c.setRepeat(Integer.parseInt(temp));
    } 
    else if (qName.equalsIgnoreCase("delay")) {
        c.setDelay(Integer.parseInt(temp));
    } 
    else if (qName.equalsIgnoreCase("fields")) {
        c.setFields(temp);
    }
}

      

0


source







All Articles