How to get jaxb to Ignore certain data during unmarshalling

后端 未结 3 1535
面向向阳花
面向向阳花 2020-12-11 22:39

I have a xml structure \"Filter\" that get unmarshalled into in a java class called \"Filter\".

The XML state looks roughly like:


  &         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 23:33

    For this use case I would create an XSLT that will convert the XML document. Then using the javax.xml.transform.* APIs, transform the XML to a JAXBResult to unmarshal the object:

    import java.io.File;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.util.JAXBResult;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.stream.StreamSource;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            TransformerFactory tf = TransformerFactory.newInstance();
    
            File xsltFile = new File("transform.xsl");
            StreamSource xsltSource = new StreamSource(xsltFile);
            Transformer transformer = tf.newTransformer(xsltSource);
    
            File xml = new File("input.xml");
            StreamSource xmlSource = new StreamSource(xml);
    
            JAXBContext jc = JAXBContext.newInstance(Filter.class);
            JAXBResult jaxbResult = new JAXBResult(jc);
    
            transformer.transform(xmlSource, jaxbResult);
    
            Filter filter = (Filter) jaxbResult.getResult();
        }
    
    }
    

    transform.xsl

    
    
        
            
                
            
        
         
           
               
           
        
    
    

提交回复
热议问题