有没有一种方法可以使用STAX解析器有效地解析具有不同类的对象的多个列表(POJO)的XML文档。 我的XML的确切结构如下(类名不是真实的)
<?xml version="1.0" encoding="utf-8"?>
<root>
<notes />
<category_alpha>
<list_a>
<class_a_object></class_a_object>
<class_a_object></class_a_object>
<class_a_object></class_a_object>
<class_a_object></class_a_object>
.
.
.
</list_a>
<list_b>
<class_b_object></class_b_object>
<class_b_object></class_b_object>
<class_b_object></class_b_object>
<class_b_object></class_b_object>
.
.
.
</list_b>
</category_alpha>
<category_beta>
<class_c_object></class_c_object>
<class_c_object></class_c_object>
<class_c_object></class_c_object>
<class_c_object></class_c_object>
<class_c_object></class_c_object>
.
.
.
.
.
</category_beta>
</root>
我一直在使用STAX Parser,即XStream库,链接:XStream
只要我的XML包含一类对象的列表,它就可以正常工作,但是我不知道如何处理包含不同类的对象的列表的XML。
任何帮助将不胜感激,如果我没有提供足够的信息或者我没有正确说明问题,请告诉我。
答案 0 :(得分:6)
您可以使用Declarative Stream Mapping (DSM)流解析库轻松地将复杂的XML转换为Java类。它使用StAX解析XML。
我跳过获取 notes 标记的操作,并在 class_x_object 标记中添加了一个字段进行演示。
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<notes />
<category_alpha>
<list_a>
<class_a_object>
<fieldA>A1</fieldA>
</class_a_object>
<class_a_object>
<fieldA>A2</fieldA>
</class_a_object>
<class_a_object>
<fieldA>A3</fieldA>
</class_a_object>
</list_a>
<list_b>
<class_b_object>
<fieldB>B1</fieldB>
</class_b_object>
<class_b_object>
<fieldB>B2</fieldB>
</class_b_object>
<class_b_object>
<fieldB>B3</fieldB>
</class_b_object>
</list_b>
</category_alpha>
<category_beta>
<class_c_object>
<fieldC>C1</fieldC>
</class_c_object>
<class_c_object>
<fieldC>C2</fieldC>
</class_c_object>
<class_c_object>
<fieldC>C3</fieldC>
</class_c_object>
</category_beta>
</root>
首先,您必须以yaml或JSON格式定义XML数据和您的类字段之间的映射。
以下是映射定义:
result:
type: object
path: /root
fields:
listOfA:
type: array
path: .*class_a_object # path is regex
fields:
fieldOfA:
path: fieldA
listOfB:
type: array
path: .*class_b_object
fields:
fieldOfB:
path: fieldB
listOfC:
type: array
path: .*class_c_object
fields:
fieldOfC:
path: fieldC
您要反序列化的Java类:
public class Root {
public List<A> listOfA;
public List<B> listOfB;
public List<C> listOfC;
public static class A{
public String fieldOfA;
}
public static class B{
public String fieldOfB;
}
public static class C{
public String fieldOfC;
}
}
用于解析XML的Java代码:
DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.XML).create(Root.class);
Root root = (Root)dsm.toObject(xmlFileContent);
// write root object as json
dsm.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(System.out, object);
此处输出:
{
"listOfA" : [ {"fieldOfA" : "A1"}, {"fieldOfA" : "A2"}, {"fieldOfA" : "A3"} ],
"listOfB" : [ {"fieldOfB" : "B1"}, {"fieldOfB" : "B2"}, "fieldOfB" : "B3"} ],
"listOfC" : [ {"fieldOfC" : "C1"}, {"fieldOfC" : "C2"}, {"fieldOfC" : "C3"} ]
}
更新:
据您的评论我了解,您希望将大型XML文件作为流读取。在读取文件时处理数据。
DSM允许您在读取XML时处理数据。
声明三种不同的功能来处理部分数据。
FunctionExecutor processA=new FunctionExecutor(){
@Override
public void execute(Params params) {
Root.A object=params.getCurrentNode().toObject(Root.A.class);
// process aClass; save to db. call service etc.
}
};
FunctionExecutor processB=new FunctionExecutor(){
@Override
public void execute(Params params) {
Root.B object=params.getCurrentNode().toObject(Root.B.class);
// process aClass; save to db. call service etc.
}
};
FunctionExecutor processC=new FunctionExecutor(){
@Override
public void execute(Params params) {
Root.C object=params.getCurrentNode().toObject(Root.C.class);
// process aClass; save to db. call service etc.
}
};
向DSM注册功能
DSMBuilder builder = new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.XML);
// register function
builder.registerFunction("processA",processA);
builder.registerFunction("processB",processB);
builder.registerFunction("processC",processC);
DSM dsm= builder.create();
Object object = dsm.toObject(xmlContent);
更改映射文件以调用注册功能
result:
type: object
path: /root
fields:
listOfA:
type: object
function: processA # when 'class_a_object' tag closed processA function will be executed.
path: .*class_a_object # path is regex
fields:
fieldOfA:
path: fieldA
listOfB:
type: object
path: .*class_b_object
function: processB# register function
fields:
fieldOfB:
path: fieldB
listOfC:
type: object
path: .*class_c_object
function: processC# register function
fields:
fieldOfC:
path: fieldC
答案 1 :(得分:3)
您可以使用Java Architecture for XML binding JAXB和Unmarshall来使用POJO类,如下所述。
首先创建POJO类(我从您的XML文件中删除了几个节点,并创建了POJO。其余部分您可以做类似的事情)。下面是我考虑过的XML。
<?xml version="1.0" encoding="utf-8"?>
<root>
<category_alpha>
<list_a>
<class_a_object></class_a_object>
<class_a_object></class_a_object>
<class_a_object></class_a_object>
<class_a_object></class_a_object>
</list_a>
<list_b>
<class_b_object></class_b_object>
<class_b_object></class_b_object>
<class_b_object></class_b_object>
<class_b_object></class_b_object>
</list_b>
</category_alpha>
</root>
以下是Root,category_alpha,list_a,list_b,class_a_object和class_b_object的POJO类
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "root")
@XmlAccessorType (XmlAccessType.FIELD)
public class Root {
@XmlElement(name = "category_alpha")
private List<CategoryAlpha> categoryAlphaList = null;
public List<CategoryAlpha> getCategoryAlphaList() {
return categoryAlphaList;
}
public void setCategoryAlphaList(List<CategoryAlpha> categoryAlphaList) {
this.categoryAlphaList = categoryAlphaList;
}
}
在下面的类中,将类似的java导入导入到上面的类中。
@XmlRootElement(name = "category_alpha")
@XmlAccessorType (XmlAccessType.FIELD)
public class CategoryAlpha {
@XmlElement(name = "list_a")
private List<ListAClass> list_a_collectionlist = null;
@XmlElement(name = "list_b")
private List<ListBClass> list_b_collectionlist = null;
public List<ListAClass> getList_a_collectionlist() {
return list_a_collectionlist;
}
public void setList_a_collectionlist(List<ListAClass> list_a_collectionlist) {
this.list_a_collectionlist = list_a_collectionlist;
}
public List<ListBClass> getList_b_collectionlist() {
return list_b_collectionlist;
}
public void setList_b_collectionlist(List<ListBClass> list_b_collectionlist) {
this.list_b_collectionlist = list_b_collectionlist;
}
}
@XmlRootElement(name = "list_a")
@XmlAccessorType (XmlAccessType.FIELD)
public class ListAClass {
@XmlElement(name = "class_a_object")
private List<ClassAObject> classAObjectList = null;
public List<ClassAObject> getClassAObjectList() {
return classAObjectList;
}
public void setClassAObjectList(List<ClassAObject> classAObjectList) {
this.classAObjectList = classAObjectList;
}
}
@XmlRootElement(name = "list_b")
@XmlAccessorType (XmlAccessType.FIELD)
public class ListBClass {
@XmlElement(name = "class_b_object")
private List<ClassBObject> classBObjectList = null;
public List<ClassBObject> getClassBObjectList() {
return classBObjectList;
}
public void setClassBObjectList(List<ClassBObject> classBObjectList) {
this.classBObjectList = classBObjectList;
}
}
@XmlRootElement(name = "class_a_object")
@XmlAccessorType (XmlAccessType.FIELD)
public class ClassAObject {
}
@XmlRootElement(name = "class_b_object")
@XmlAccessorType (XmlAccessType.FIELD)
public class ClassBObject {
}
这是主要班级
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class UnmarshallMainClass {
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
// This root object contains all the list of objects you are looking for
Root emps = (Root) jaxbUnmarshaller.unmarshal( new File("sample.xml") );
}
}
通过使用根对象和其他对象中的吸气剂,您可以检索根内所有对象的列表,如下所示。
List<CategoryAlpha> categoryAlphaList = emps.getCategoryAlphaList();
答案 2 :(得分:0)
我已经为提供的示例创建了一个解析器。 https://github.com/sbzDev/stackoverflow/tree/master/question56087924
import com.thoughtworks.xstream.annotations.XStreamAlias;
import java.util.List;
@XStreamAlias("root")
public class Root {
String notes;
@XStreamAlias("category_alpha")
CategoryAlpha categoryAlpha;
@XStreamAlias("category_beta")
List<C> listC;
static class CategoryAlpha {
@XStreamAlias("list_a")
List<A> listA;
@XStreamAlias("list_b")
List<B> listB;
}
@XStreamAlias("class_a_object")
static class A {
}
@XStreamAlias("class_b_object")
static class B {
}
@XStreamAlias("class_c_object")
static class C {
}
}
解析器:
import com.thoughtworks.xstream.XStream;
public class SampleRootParser {
public Root parse(String xmlContent){
XStream xstream = new XStream();
xstream.processAnnotations(Root.class);
return (Root)xstream.fromXML(xmlContent);
}
}
也许您可以提供实际的XML和预期的结果?