gpt4 book ai didi

java - 如何解析带有与父元素同名的子元素的xml

转载 作者:数据小太阳 更新时间:2023-10-29 02:38:53 26 4
gpt4 key购买 nike

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

关于java - 如何解析带有与父元素同名的子元素的xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45199165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com