JAXB unmarshal with declared type does not populate the resulting object with data

前端 未结 4 1063
孤街浪徒
孤街浪徒 2021-01-21 16:51

I am trying to unmarshal a given XML:


 rewriting of file
 toolA

        
相关标签:
4条回答
  • 2021-01-21 17:28

    Try to remove following code, and try again:

      @XmlType(name = "", propOrder = {
         "tx",
         "toolId",
         "toolVendor",
         "toolVersion",
         "userName",
         "commonProperties",
         "extensions"
        })
    
    0 讨论(0)
  • 2021-01-21 17:48

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

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

    For More Information

    • http://blog.bdoughan.com/2012/11/applying-namespace-during-jaxb-unmarshal.html
    0 讨论(0)
  • 2021-01-21 17:49

    How do you have annotated the classes BaseName , BaseVersion and TX?

    If you have not annotated these classes, you will annotated the String inside of this classes as @XmlValue

    0 讨论(0)
  • 2021-01-21 17:54

    Change BaseName and TX to String and it shall work. As it is the xml doesn't comply with your schema, which is represented by the class in this case.

    0 讨论(0)
提交回复
热议问题