Dealing with poorly designed XML with JAXB

后端 未结 4 964
野的像风
野的像风 2021-01-13 05:21

I\'m currently working on replacing a legacy system with JAXB and I\'m running into problem with parsing the XML. The number one requirement of the system is that it must be

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-13 05:42

    If you want to handle at the s# items as a collection:

    import java.io.Serializable;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name="xx")
    public class XMLXx implements Serializable {
    
        private static final long serialVersionUID = 4064597372833234503L;
    
        private List sites;
    
        @XmlElement(name="s")
        public List getSites() {
            return sites;
        }
    
        public void setSites(List sites) {
            this.sites = sites;
        }
    
    }
    

    Then you could do something like to fool JAXB into thinking all the elements (s1, s2, etc) are actually called s:

    import java.io.FileInputStream;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.stream.XMLInputFactory;
    import javax.xml.stream.XMLStreamReader;
    import javax.xml.stream.util.StreamReaderDelegate;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(XMLXx.class);
    
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("input.xml"));
            xsr = new SiteStreamReaderDelegate(xsr);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            XMLXx object = (XMLXx) unmarshaller.unmarshal(xsr);
            System.out.println(object.getSites().size());
    
        }
    
        private static class SiteStreamReaderDelegate extends StreamReaderDelegate {
    
            public SiteStreamReaderDelegate(XMLStreamReader xsr) {
                super(xsr);
            }
    
            @Override
            public String getLocalName() {
                String localName = super.getLocalName();
                if(localName.startsWith("s")) {
                    return "s";
                }
                return localName;
            }
    
        }
    }
    

    For a similar example see:

    • http://bdoughan.blogspot.com/2010/12/case-insensitive-unmarshalling.html

提交回复
热议问题