如何解析 xml 与父元素同名的子元素

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

我有一个 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>  

在另一个 call 元素中可以存在 call 个复杂元素,例如 api1。这个 xml 结构有效吗?如果是这样,我如何使用 SAX

解析此 xml
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();
    }

}  

我应该在哪里打电话 Workflow.add(call) & Call.add(call)

编辑

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

我对如何操作很感兴趣,解决方案似乎很简单。要使用核心解决方案,您可以查看我的 commit.

如果您只需要核心答案,请查看下面的代码:

工作流程

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;

通话

@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;

}

输入点例如

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 - 你的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>

我希望通过共享示例可以清楚,但如有任何问题,请随时提出。

我认为如果 xml 已经解析,您可以处理名称比较。


为了使对象更有用,您可以添加 getter\setter\equals\hashCode 等等...

我已经通过以下方法解决了这个问题。 修改xml 来标识父节点和子节点。

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>

要解析的代码

@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);
    }
}