用Java将这个xml文件解析为指定的JSON最简单的方法是什么?
正文和摘要由一系列段落组成,每个段落都可以有在线引用。我最担心的是该段所载的资料。我现在可以把引文留空。还有一件事,每个段落可以属于一个主要部分和多个小节 我被要求将小节层次结构实现为“subsection1.subsection2.subsection3”,它通过用点分隔小节来表示层次结构。我首先考虑使用XOM将xml解析为POJO,然后使用GSON将其序列化为JSON。有没有更容易的方法 (有点长) 全文文档的JSON模式用Java将这个xml文件解析为指定的JSON最简单的方法是什么?,xml,parsing,Xml,Parsing,正文和摘要由一系列段落组成,每个段落都可以有在线引用。我最担心的是该段所载的资料。我现在可以把引文留空。还有一件事,每个段落可以属于一个主要部分和多个小节 我被要求将小节层次结构实现为“subsection1.subsection2.subsection3”,它通过用点分隔小节来表示层次结构。我首先考虑使用XOM将xml解析为POJO,然后使用GSON将其序列化为JSON。有没有更容易的方法 (有点长) 全文文档的JSON模式 { "paper_id": <str>,
{
"paper_id": <str>, # 40-character sha1 of the PDF
"metadata": {
"title": <str>,
"authors": [ # list of author dicts, in order
{
"first": <str>,
"middle": <list of str>,
"last": <str>,
"suffix": <str>,
"affiliation": <dict>,
"email": <str>
},
...
],
"abstract": [ # list of paragraphs in the abstract
{
"text": <str>,
"cite_spans": [ # list of character indices of inline citations
# e.g. citation "[7]" occurs at positions 151-154 in "text"
# linked to bibliography entry BIBREF3
{
"start": 151,
"end": 154,
"text": "[7]",
"ref_id": "BIBREF3"
},
...
],
"ref_spans": <list of dicts similar to cite_spans>, # e.g. inline reference to "Table 1"
"section": "Abstract"
},
...
],
"body_text": [ # list of paragraphs in full body
# paragraph dicts look the same as above
{
"text": <str>,
"cite_spans": [],
"ref_spans": [],
"eq_spans": [],
"section": "Introduction"
},
...
{
...,
"section": "Conclusion"
}
],
"bib_entries": {
"BIBREF0": {
"ref_id": <str>,
"title": <str>,
"authors": <list of dict> # same structure as earlier,
# but without `affiliation` or `email`
"year": <int>,
"venue": <str>,
"volume": <str>,
"issn": <str>,
"pages": <str>,
"other_ids": {
"DOI": [
<str>
]
}
},
"BIBREF1": {},
...
"BIBREF25": {}
},
"ref_entries":
"FIGREF0": {
"text": <str>, # figure caption text
"type": "figure"
},
...
"TABREF13": {
"text": <str>, # table caption text
"type": "table"
}
},
"back_matter": <list of dict> # same structure as body_text
}
}
{
“纸张id”:,PDF文件中40个字符的sha1
“元数据”:{
“标题”:,
“作者”:[#作者口述列表,按顺序排列
{
“第一”:,
“中间”:,
“最后”:,
“后缀”:,
“从属关系”:,
“电子邮件”:
},
...
],
“摘要”:[#摘要中的段落列表
{
“案文”:,
“引文跨度”:[#内联引文的字符索引列表
#例如,引文“[7]”出现在“文本”的151-154处
#链接到书目条目BIBREF3
{
“开始”:151,
“结束”:154,
“文本”:“[7]”,
“参考id”:“BIBREF3”
},
...
],
“参考跨度”:,例如对“表1”的内联引用
“节”:“摘要”
},
...
],
“正文”:[正文中的段落列表]
#段落注释看起来和上面一样
{
“案文”:,
“引证跨度”:[],
“参考跨度”:[],
“eq_跨度”:[],
“部分”:“导言”
},
...
{
...,
“部分”:“结论”
}
],
“bib_条目”:{
“BIBREF0”:{
“参考id”:,
“标题”:,
“作者”:#与之前的结构相同,
#但没有“联系”或“电子邮件”`
“年”:,
“地点”:,
“卷”:,
“issn”:,
“页数”:,
“其他_id”:{
“内政部”:[
]
}
},
“BIBREF1”:{},
...
“BIBREF25”:{}
},
“参考条目”:
“FIGREF0”:{
“文本”:,#图形标题文本
“类型”:“图形”
},
...
“表13”:{
“文本”:,#表格标题文本
“类型”:“表”
}
},
“背景内容”:#与正文结构相同
}
}
“最简单”在很大程度上取决于你的技能
我会使用XSLT3.0来实现这一点。在XSLT 3.0中,有几种方法可以完成任务;我想我应该使用构造映射和数组的模板规则来完成,然后使用JSON序列化方法将映射和数组的结果结构转换为JSON。以下是您可能使用的两个模板规则作为示例:
<xsl:output method="json" indent="yes"/>
<xsl:template match="tei:biblStruct/tei:analytic">
<xsl:map:entry key="'authors'" select="f:array-from(tei:author)"/>
</xsl:template>
<xsl:template match="tei:author">
<xsl:map>
<xsl:if test="tei:persName/tei:firstName">
<xsl:map:entry key="'first'" select="string(tei:persName/tei:firstName)"/>
</xsl:if>
<xsl:if test="tei:persName/tei:lastName">
<xsl:map:entry key="'last'" select="string(tei:persName/tei:lastName)"/>
</xsl:if>
...
</xsl:template>
我以前从未听说过XSLT。我将寻找一些关于它的教程。不过这似乎很简单。我认为使用xml->html->JSON比使用xml->POJO->JSON更容易?XSLT方法直接从xml转换为JSON,而不是通过中间形式(html或Java),我认为这使它更简单。XSLT确实有一个学习曲线,但它应该是每个XML从业者工具包的标准部分。由于我以前使用过XOM,所以我最终选择了XOM作为第一个版本。对于第二个版本,我将使用XSLT。谢谢你提供的宝贵信息,没关系。使用XOM而不是DOM是一个很好的选择,我希望更多的人会意识到DOM有更好的替代品。
<xsl:function name="f:array-from" as="array(*)">
<xsl:param name="elements" select="element(*)*"/>
<xsl:variable name="out" as="item()*">
<xsl:apply-templates select="$elements"/>
</xsl:variable>
<xsl:sequence select="array{$out}"/>
</xsl:function>