具有声明类型的JAXB unmarshal不会使用数据填充结果对象

时间:2013-06-20 13:28:29

标签: java xml jaxb

我正在尝试解组给定的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类,请参见:

@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的代码如下:

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,这不是理想的结果。

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

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

  

意外元素(uri:“”,local:“TX”)。预期元素是&lt; {htp://www.example.com/mdf/v4} tool_id&gt;,&lt; {htp://www.example.com/mdf/v4} TX&gt ;,&lt; {htp:// www.www.example.com/mdf/v4}common_properties>,<{htp://www.example.com/mdf/v4}tool_version>,<{htp://www.example.com/mdf/ V4}扩展&GT;,&LT; {HTP://www.www.example.com/mdf/v4} tool_vendor&GT;,&LT; {HTP://www.www.example.com/mdf/v4} USER_NAME&GT;

     

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

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

EDIT2:

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

4 个答案:

答案 0 :(得分:3)

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

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);
    }

}

下面是一个如何在解组期间利用XMLFilter的示例。

    // 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();

了解更多信息

答案 1 :(得分:0)

BaseNameTX更改为字符串,它可以正常工作。因为它是xml不符合您的架构,在这种情况下由类表示。

答案 2 :(得分:0)

尝试删除以下代码,然后重试:

  @XmlType(name = "", propOrder = {
     "tx",
     "toolId",
     "toolVendor",
     "toolVersion",
     "userName",
     "commonProperties",
     "extensions"
    })

答案 3 :(得分:0)

你如何注释类BaseName,BaseVersion和TX?

如果您没有注释这些类,则会将此类中的String注释为@XmlValue