我的目标是将XML文件解组为一个Java对象。 我的XML看起来像:
window.location.href = "..."
我只从info1-info3收到信息。 BusinessOperationEvent包含XML文件中的所有XMLElements。 解析器类:
public
BusinessOperationEvent是用于从XML收集信息的Java对象:
<root>
<info1>
<info2>
<info3>
<nested1>
<nested2>
<info4>
<info5>
<nested2>
<nested1>
<root>
XML:
public class Parser {
public static void main(String[] args){
try {
ClassLoader classLoader = Parser.class.getClassLoader();
File xml = new File(classLoader.getResource("XML.xml").getFile());
JAXBContext jaxbContext = JAXBContext.newInstance(BusinessOperationEvent.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
BusinessOperationEvent event = (BusinessOperationEvent) unmarshaller.unmarshal(xml);
System.out.println(event);
} catch (JAXBException e) {
throw new RuntimeException("Error unmarshalling XML");
}
}
}
我在Java对象中只获得了与Method XML标签相关的信息,而不仅仅是nulls。
答案 0 :(得分:0)
您应该构建类层次结构等消息和事件类
@XmlRootElement(name = "SrvPutBusinessOperationEventNf")
public class BusinessOperationEvent
{
@XmlElement(name = "RqUID")
private String rqUID;
@XmlElement(name = "RqTm")
private String rqTm;
@XmlElement(name = "sPName")
private String sPName;
@XmlElement(name = "SystemId")
private String systemId;
@XmlElement(name = "Method")
private String method;
@XmlElement(name = "Message")
Message message
}
public class Message
{
@XmlElement(name = "Event")
Event event;
}
等等