使用 JAXB 提取 XML 元素的内部文本
问题
给定以下 XML 配置文件:
<main>
<name>JET</name>
<maxInstances>5</maxInstances>
<parameters>
<a>1</a>
<b>
<b1>test1</b1>
<b2>test2</b2>
</b>
</parameters>
</main>
我需要提取 name 和 maxInstances 元素的值,然后提取parameters 元素的整个内部文本。例如
name = "JET"
maxInstances = 5
parameters = "<a>1</a><b><b1>test1</b1><b2>test2</b2></b>"
,最终参数块可以包含任何格式良好的XML。
尝试的解决方案
以下代码适用于 name 和 maxInstances,但不适用于参数:
@XmlRootElement(name="main")
public class Main {
@XmlElement(name="name", required="true")
private String name;
@XmlElement(name="maxInstances", required="true")
private Integer maxInstances;
@XmlElement(name="parameters")
private String parameters;
}
我尝试根据以下想法寻找解决方案,但找不到合适的解决方案。
是否可以使用不同的类型来表示可以解析以生成字符串的 XML 树的参数对象?例如
@XmlElement(name="parameters")
private XmlNodeObject parametersNode;
public String getParameters() {
// Collapse node to single line of text
return innerText;
}
或者我需要使用某种不同类型的注释吗?
@XmlSpecialAnnotation(...)
@XmlElement(name="parameters")
private String parameters;
我需要切换到不同风格的解析器吗?使用两种风格的解析器是好还是坏主意?
Problem
Given the following XML configuration file:
<main>
<name>JET</name>
<maxInstances>5</maxInstances>
<parameters>
<a>1</a>
<b>
<b1>test1</b1>
<b2>test2</b2>
</b>
</parameters>
</main>
I need to extract the value of the name and maxInstances elements and then the whole inner text of the parameters element. e.g.
name = "JET"
maxInstances = 5
parameters = "<a>1</a><b><b1>test1</b1><b2>test2</b2></b>"
Ultimately the parameters block can contain any well formed XML.
Attempted Solution
The following code works for name and maxInstances but not parameters:
@XmlRootElement(name="main")
public class Main {
@XmlElement(name="name", required="true")
private String name;
@XmlElement(name="maxInstances", required="true")
private Integer maxInstances;
@XmlElement(name="parameters")
private String parameters;
}
I've tried looking at solutions based on the following ideas but can't find something appropriate.
Is there a different type I can use for the parameters object representing the XML Tree that I could parse to produce a string? e.g.
@XmlElement(name="parameters")
private XmlNodeObject parametersNode;
public String getParameters() {
// Collapse node to single line of text
return innerText;
}
Or do I need to use some different kind of annotation?
@XmlSpecialAnnotation(...)
@XmlElement(name="parameters")
private String parameters;
Do I need to switch to a different style of parser? Is it a good/bad idea to use two styles of parser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
@XmlAnyElement
注释 如 bmargulies 所描述。要映射到问题中的对象模型,您可以利用DOMHandler
。主要
参数处理程序
演示
You can use the
@XmlAnyElement
annotation as described by bmargulies. To map to the object model in your question you can leverage aDOMHandler
.Main
ParameterHandler
Demo
最接近的方法是将“参数”映射到 DOM 树,方法是将变量声明为
org.w3c.dom.Node
。 (实际上,声明一个 JAXBElement)。有关详细信息,请参阅 http://jaxb.java.net/guide/Avoid_strong_databinding.html 。这为您提供了模式优先的处方,您可以通过 xsd2java 运行该模式并查看输出来了解如何从 java 开始。
要获取字符串,您必须从 DOM 进行序列化。
或者,更具体地说:
此页面这里描述了xsd:any处理,因此
其中
Element
是DOM接口。The closest you could come is to map 'parameters' to a DOM tree, by declaring the variable to be
org.w3c.dom.Node
. (Actually, declaring a JAXBElement).For details, see http://jaxb.java.net/guide/Avoid_strong_databinding.html. That gives you the schema-first prescription, you can see how to start from java by running that schema through xsd2java and looking at the output.
To get a string you'll have to serialize from the DOM.
Or, even more specifically:
this page here describes xsd:any processing, and thus
Where
Element
is the DOM interface.