将 "XML element" 解组为字符串属性
Unmarshall "XML element" into string attribute
使用 JAXB 我想解组下一个 xml:
<con:fault xmlns:con="http://www.bea.com/wli/sb/context">
<con:errorCode>BEA-382505</con:errorCode>
<con:reason>OSB Validate action failed validation</con:reason>
<con:details>
.... MORE XML tags ...
<con:details>
</con:fault>
我想在下一个对象中解组:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Fault", propOrder = {
"errorCode",
"reason",
"details"
})
public class Fault {
@XmlElement(required = true)
protected String errorCode;
@XmlElement(required = true)
protected String reason;
@XmlElement(required = true)
protected String details;
... getters and setters ...
}
Errorcode 和 reason 属性已正确填充,但 details 属性未填充“con:details”元素内的 XML 内容。
谢谢
如果 <con:details>
标签有子 XML 元素,details 变量应该是 Details class 的对象,而不是字符串。详细信息 class 应包含 <con:details>
标记的子元素。
编辑:
@XmlJavaTypeAdapter 可用于定义 XML 用于自定义封送处理的适配器。
Fault.class
@XmlRootElement
public class Fault {
@XmlElement(required = true)
protected String errorCode;
@XmlElement(required = true)
protected String reason;
@XmlElement(name = "details")
@XmlJavaTypeAdapter(value = DetailsHandler.class)
protected String details;
}
DetailsHandler.class
public class DetailsHandler extends XmlAdapter<Object, String> {
/**
* Factory for building DOM documents.
*/
private final DocumentBuilderFactory docBuilderFactory;
/**
* Factory for building transformers.
*/
private final TransformerFactory transformerFactory;
public DetailsHandler() {
docBuilderFactory = DocumentBuilderFactory.newInstance();
transformerFactory = TransformerFactory.newInstance();
}
@Override
public String unmarshal(Object o) throws Exception {
Element titleElement = (Element) o;
// If there is no child nodes, return empty string
if (!titleElement.hasChildNodes()) {
return "";
}
// Getting the "details" child elements
NodeList anchorElements = titleElement.getChildNodes();
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
for (int i = 0; i < anchorElements.getLength()-1; i++) {
// Fetching the child elements one by one
Element anchor = (Element) anchorElements.item(i);
// Creating a DOMSource as input for the transformer
DOMSource source = new DOMSource(anchor);
// Default transformer: identity tranformer (doesn't alter input)
Transformer transformer = transformerFactory.newTransformer();
// This is necessary to avoid the <?xml ...?> prolog
transformer.setOutputProperty("omit-xml-declaration", "yes");
// Transform to a StringWriter
transformer.transform(source, result);
}
// Returning result as string
return stringWriter.toString();
}
@Override
public Object marshal(String s) throws Exception {
// custom implementation if required
return null;
}
}
可以提供更多详细信息。
使用 JAXB 我想解组下一个 xml:
<con:fault xmlns:con="http://www.bea.com/wli/sb/context">
<con:errorCode>BEA-382505</con:errorCode>
<con:reason>OSB Validate action failed validation</con:reason>
<con:details>
.... MORE XML tags ...
<con:details>
</con:fault>
我想在下一个对象中解组:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Fault", propOrder = {
"errorCode",
"reason",
"details"
})
public class Fault {
@XmlElement(required = true)
protected String errorCode;
@XmlElement(required = true)
protected String reason;
@XmlElement(required = true)
protected String details;
... getters and setters ...
}
Errorcode 和 reason 属性已正确填充,但 details 属性未填充“con:details”元素内的 XML 内容。
谢谢
如果 <con:details>
标签有子 XML 元素,details 变量应该是 Details class 的对象,而不是字符串。详细信息 class 应包含 <con:details>
标记的子元素。
编辑:
@XmlJavaTypeAdapter 可用于定义 XML 用于自定义封送处理的适配器。
Fault.class
@XmlRootElement
public class Fault {
@XmlElement(required = true)
protected String errorCode;
@XmlElement(required = true)
protected String reason;
@XmlElement(name = "details")
@XmlJavaTypeAdapter(value = DetailsHandler.class)
protected String details;
}
DetailsHandler.class
public class DetailsHandler extends XmlAdapter<Object, String> {
/**
* Factory for building DOM documents.
*/
private final DocumentBuilderFactory docBuilderFactory;
/**
* Factory for building transformers.
*/
private final TransformerFactory transformerFactory;
public DetailsHandler() {
docBuilderFactory = DocumentBuilderFactory.newInstance();
transformerFactory = TransformerFactory.newInstance();
}
@Override
public String unmarshal(Object o) throws Exception {
Element titleElement = (Element) o;
// If there is no child nodes, return empty string
if (!titleElement.hasChildNodes()) {
return "";
}
// Getting the "details" child elements
NodeList anchorElements = titleElement.getChildNodes();
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
for (int i = 0; i < anchorElements.getLength()-1; i++) {
// Fetching the child elements one by one
Element anchor = (Element) anchorElements.item(i);
// Creating a DOMSource as input for the transformer
DOMSource source = new DOMSource(anchor);
// Default transformer: identity tranformer (doesn't alter input)
Transformer transformer = transformerFactory.newTransformer();
// This is necessary to avoid the <?xml ...?> prolog
transformer.setOutputProperty("omit-xml-declaration", "yes");
// Transform to a StringWriter
transformer.transform(source, result);
}
// Returning result as string
return stringWriter.toString();
}
@Override
public Object marshal(String s) throws Exception {
// custom implementation if required
return null;
}
}