gpt4 book ai didi

xml - 使用 JAXB 使用 XmlElement 和 XmlAttributes 解析和 XML

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

我无法使用 JAXB 解码以下 xml。请帮我解决这个问题。

我的 xml 看起来像这样:

   <powerchanges>
<initialState creationTime="14.08.2012 16:24:01" status="poweredOff" />
<powerchange changeTime="14.08.2012 16:24:49" status="Powered On" />
<powerchange changeTime="27.09.2012 02:17:23" status="Powered Off" />
<powerchange changeTime="27.09.2012 02:51:13" status="Powered On" />
<powerchange changeTime="29.09.2012 05:06:48" status="Powered Off" />
<powerchange changeTime="29.09.2012 07:40:24" status="Powered On" />
</powerchanges>

我正在尝试使用 JAXB 解码上述 xml。下面是我的 java 类:

PowerChanges.java

@XmlRootElement(name="powerchanges")
public class PowerChanges {
@XmlElement(name = "initialState")
private InitialState initialState;

@XmlElement(name = "powerchange")
private ArrayList<PowerChange> powerChangeList;


public InitialState getInitialState() {
return initialState;
}
public void setInitialState(InitialState initialState) {
this.initialState = initialState;
}

public ArrayList<PowerChange> getPowerChangeList() {
return powerChangeList;
}
public void setPowerChangeList(ArrayList<PowerChange> powerChangeList) {
this.powerChangeList = powerChangeList;
}

PowerChange.java:

public class PowerChange {

private String changeTime;
private String status;

@XmlAttribute
public String getChangeTime() {
return changeTime;
}
public void setChangeTime(String changeTime) {
this.changeTime = changeTime;
}
@XmlAttribute
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

}

初始状态.java

public class InitialState {

private String creationTime;
private String status;

@XmlAttribute
public String getCreationTime() {
return creationTime;
}
public void setCreationTime(String creationTime) {
this.creationTime = creationTime;
}
@XmlAttribute
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

最佳答案

默认 JAXB (JSR-222)实现在属性(获取/设置方法)上寻找元数据。由于您已经在 PowerChanges 类上注释了字段,因此您需要使用 @XmlAccessorType(XmlAccessType.FIELD) 注释该类。我对您的 PowerChanges 类进行了此更改,并且能够完美地从您的帖子中读取 XML。

解决方案 - 更改 PowerChanges 以使用 Field Access

import java.util.ArrayList;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "powerchanges")
@XmlAccessorType(XmlAccessType.FIELD)
public class PowerChanges {
@XmlElement(name = "initialState")
private InitialState initialState;

@XmlElement(name = "powerchange")
private ArrayList<PowerChange> powerChangeList;

public InitialState getInitialState() {
return initialState;
}

public void setInitialState(InitialState initialState) {
this.initialState = initialState;
}

public ArrayList<PowerChange> getPowerChangeList() {
return powerChangeList;
}

public void setPowerChangeList(ArrayList<PowerChange> powerChangeList) {
this.powerChangeList = powerChangeList;
}
}

替代解决方案 - 在 PowerChanges 上注释属性

或者,您可以将注释移动到 get 方法。

import java.util.ArrayList;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "powerchanges")
public class PowerChanges {
private InitialState initialState;

private ArrayList<PowerChange> powerChangeList;

@XmlElement(name = "initialState")
public InitialState getInitialState() {
return initialState;
}

public void setInitialState(InitialState initialState) {
this.initialState = initialState;
}

@XmlElement(name = "powerchange")
public ArrayList<PowerChange> getPowerChangeList() {
return powerChangeList;
}

public void setPowerChangeList(ArrayList<PowerChange> powerChangeList) {
this.powerChangeList = powerChangeList;
}
}

了解更多信息


您当前的模型可能会遇到非常类似于以下内容的异常。将来发布您看到的任何异常消息将有助于人们为您提供更好的答案。

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "initialState"
this problem is related to the following location:
at public forum13229013.InitialState forum13229013.PowerChanges.getInitialState()
at forum13229013.PowerChanges
this problem is related to the following location:
at private forum13229013.InitialState forum13229013.PowerChanges.initialState
at forum13229013.PowerChanges
Class has two properties of the same name "powerChangeList"
this problem is related to the following location:
at public java.util.ArrayList forum13229013.PowerChanges.getPowerChangeList()
at forum13229013.PowerChanges
this problem is related to the following location:
at private java.util.ArrayList forum13229013.PowerChanges.powerChangeList
at forum13229013.PowerChanges

at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:451)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:283)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1148)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at forum13229013.Demo.main(Demo.java:12)

关于xml - 使用 JAXB 使用 XmlElement 和 XmlAttributes 解析和 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13229013/

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