具有声明类型的JAXB unmarshal不会使用数据填充结果对象 [英] JAXB unmarshal with declared type does not populate the resulting object with data

查看:94
本文介绍了具有声明类型的JAXB unmarshal不会使用数据填充结果对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解组给定的XML:

I am trying to unmarshal a given XML:

<FHcomment>
 <TX>rewriting of file</TX>
 <tool_id>toolA</tool_id>
 <tool_vendor>Company</tool_vendor>
 <tool_version>1.7.36.0</tool_version>
</FHcomment>

模式已经编译为JAXB类,请参见:

The schema has already been compiled to JAXB classes, see here:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
 "tx",
 "toolId",
 "toolVendor",
 "toolVersion",
 "userName",
 "commonProperties",
 "extensions"
})
@XmlRootElement(name = "FHcomment")
public class FHcomment {

@XmlElement(name = "TX", required = true)
protected TX tx;
@XmlElement(name = "tool_id", required = true)
protected BaseName toolId;
@XmlElement(name = "tool_vendor", required = true)
protected BaseName toolVendor;
@XmlElement(name = "tool_version", required = true)
protected BaseVersion toolVersion;
@XmlElement(name = "user_name")
protected BaseName userName;
@XmlElement(name = "common_properties")
protected CommonPropertiesType commonProperties;
protected ExtensionsType extensions;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();

.....
/*
 * GETTERS and SETTERS for the fields have been removed here
 */
.....
}

我解组XML的代码如下:

My code to unmarshal the XML is as follows:

JAXBContext jc = JAXBContext.newInstance(FHcomment.class);
String s = "<FHcomment>....</Fhcomment>";
Unmarshaller unmarshaller = jc.createUnmarshaller();
XMLInputFactory fac = XMLInputFactory.newFactory();
XMLStreamReader xsr = fac.createXMLStreamReader(new StringReader(s));
JAXBElement<FHcomment> foo = unmarshaller.unmarshal(xsr, FHcomment.class);
FHcomment val = foo.getValue();

问题:生成的FHcomment对象不包含FHcomment的子元素。所有都是null,这不是理想的结果。

Problem: The resulting FHcomment object does not contain the children elements of FHcomment. All are null which is not the desired result.

如何告诉JAXB将给定的XML完全解组为对象?

How can I tell JAXB to completely unmarshal the given XML into an object?

编辑:在向Unmsarshaller添加ValidationHandler后,我更接近问题了:

After adding a ValidationHandler to the Unmsarshaller, I got closer to the problem:


意外元素(uri:,local:TX)。预期元素是< {htp://www.example.com/mdf/v4} tool_id>,< {htp://www.example.com/mdf/v4} TX>,< {htp:// www.www.example.com/mdf/v4}common_properties>,<{htp://www.example.com/mdf/v4}tool_version>,<{htp://www.example.com/mdf/ V4}扩展>,< {HTP://www.www.example.com/mdf/v4} tool_vendor>,< {HTP://www.www.example.com/mdf/v4}用户名>

unexpected element (uri:"", local:"TX"). Expected elements are <{htp://www.example.com/mdf/v4}tool_id>,<{htp://www.example.com/mdf/v4}TX>,<{htp://www.www.example.com/mdf/v4}common_properties>,<{htp://www.example.com/mdf/v4}tool_version>,<{htp://www.example.com/mdf/v4}extensions>,<{htp://www.www.example.com/mdf/v4}tool_vendor>,<{htp://www.www.example.com/mdf/v4}user_name>

意外元素(uri:,local:tool_id)。预期的元素是....

unexpected element (uri:"", local:"tool_id"). Expected elements are....

事实证明,JAXB不喜欢提供的XML不包含名称空间信息这一事实。那么如何告诉unmarshaller忽略命名空间?

It turns out JAXB does not like the fact that the provided XML does not contain namespace information.. So how do I tell the unmarshaller to ignore the namespaces?

EDIT2:

经过一些研究我可以没有找到一种方法可以在没有命名空间验证的情况下欺骗JAXB。我在 http:// cooljavablogs上使用了该教程。 blogspot.de/2008/08/how-to-instruct-jaxb-to-ignore.html 以规避我的问题。不是一个很好的解决方案,但是最好的...

After some more research I could not find a way to trick JAXB into working without namespace verification. I used the tutorial at http://cooljavablogs.blogspot.de/2008/08/how-to-instruct-jaxb-to-ignore.html to circumvent my problem. Not a nice solution but the best at hand...

推荐答案

您的XML文档与在中定义的命名空间限定条件不匹配您的映射(请参阅: http://blog.bdoughan.com/2010/08/ JAXB的namespaces.html )。您可以利用 XMLFilter 在解组操作期间将命名空间应用于XML文档。

Your XML document does not match the namespace qualification that was defined in your mappings (see: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html). You could leverage an XMLFilter to apply a namespace to your XML document during the unmarshal operation.

import org.xml.sax.*;
import org.xml.sax.helpers.XMLFilterImpl;

public class NamespaceFilter extends XMLFilterImpl {

    private static final String NAMESPACE = "htp://www.example.com/mdf/v4";

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        super.endElement(NAMESPACE, localName, qName);
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        super.startElement(NAMESPACE, localName, qName, atts);
    }

}

以下是您的一个示例在unmarshal期间会利用 XMLFilter

Below is an example of how you would leverage the XMLFilter during an unmarshal.

    // Create the XMLFilter
    XMLFilter filter = new NamespaceFilter();

    // Set the parent XMLReader on the XMLFilter
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    filter.setParent(xr);

    // Set UnmarshallerHandler as ContentHandler on XMLFilter
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    UnmarshallerHandler unmarshallerHandler = unmarshaller
            .getUnmarshallerHandler();
    filter.setContentHandler(unmarshallerHandler);

    // Parse the XML
    InputSource xml = new InputSource("input.xml");
    filter.parse(xml);
    Object result = unmarshallerHandler.getResult();

更多信息

  • http://blog.bdoughan.com/2012/11/applying-namespace-during-jaxb-unmarshal.html

这篇关于具有声明类型的JAXB unmarshal不会使用数据填充结果对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆