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

时间:2017-07-19 19:10:59

标签: java xml sax

我有一个像这样的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)&amp; 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> 

2 个答案:

答案 0 :(得分:1)

我对如何做到感兴趣,解决方案看起来非常简单。要使用核心解决方案,您可以查看我的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等等......

答案 1 :(得分:0)

我通过采用以下方法解决了这个问题。 修改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);
    }
}