【发布时间】:2018-03-22 13:58:29
【问题描述】:
在我的 java(/spring/hibernate) web 应用程序中,我正在与这样的 XML 竞争(出于示例目的,我已经将它简化了很多 - 我无法修改 XML,因为我从第三方接收它- 我只控制客户端代码和客户端域对象 - 也没有 XSD 或 WSDL 来表示此 XML):
<?xml version="1.0" encoding="utf-16"?>
<Records count="22321">
<Metadata>
<FieldDefinitions>
<FieldDefinition id="4444" name="name" />
<FieldDefinition id="5555" name="hair_color" />
<FieldDefinition id="6666" name="shoe_size" />
<FieldDefinition id="7777" name="last_comment"/>
<!-- around 100 more of these -->
</FieldDefinitions>
</Metadata>
<!-- Several complex object we don't care about here -->
<Record contentId="88484848475" >
<Field id="4444" type="9">
<Reference id="56765">Joe Bloggs</Reference>
</Field>
<Field id="5555" type="4">
<ListValues>
<ListValue id="290711" displayName="Red">Red</ListValue>
</ListValues>
</Field>
<Field id="6666" type="4">
<ListValues>
<ListValue id="24325" displayName="10">10</ListValue>
</ListValues>
</Field>
<Field id="7777" type="1">
<P>long form text here with escaped XML here too
don't need to process or derefernce the xml here,
just need to get it as string in my pojo<P>
</Field>
</Record>
<Record><!-- another record obj here with same fields --> </Record>
<Record><!-- another record obj here with same fields--> </Record>
<!-- thousands more records in the sameish format -->
</Records>
XML 包含一个“记录”元素,其中包含一些元数据,然后是许多“记录”元素。每个记录元素都包含许多“字段”条目。
我的目标是使用 JAXB 将此 XML 解组为大量“记录”对象。所以我可以这样做:
List<Record> unmarhsalledRecords = this.getRecordsFromXML(stringOfXmlShownAbove)
每条记录如下所示:
public class Record {
private String name;
private String hairColor;
private String shoeSize;
private String lastComment;
//lots more fields
//getters and setters for these fields
}
但是,我从来不需要取消引用 jaxb 中的字段名称 - 使用 jaxb 甚至可能 - 还是我需要使用 stax 解析器编写一些杂乱/难以维护的代码?
我在网上找到的所有例子都没有涉及到这样的事情 - 任何帮助都将不胜感激。
谢谢!
【问题讨论】:
标签: java xml xml-parsing jaxb