【问题标题】:How to parse xml with child elements with the same name as parent如何使用与父元素同名的子元素解析xml
【发布时间】:2017-07-19 19:10:58
【问题描述】:

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

【问题讨论】:

标签: java xml sax


【解决方案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 等等...

【讨论】:

  • 我会试试这个解决方案。同时,我通过修改xml找到了另一种解决方案。如果可行,您的解决方案会更干净。
  • 在发布答案之前,我已经检查过了。另外我刚刚添加了file with terminal output
  • 如果 Call xml 在问题的 EDIT 部分中更改,我应该如何修改 Call 类来解析这个结构?
【解决方案2】:

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

工作流.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);
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多