xerces jar和dom,jaxb解析冲突的解决方法

1.采用dom解析的时候,遇到Xerces.jar,会出现
Exception in thread "main" java.lang.AbstractMethodError: org.apache.crimson.tree.ElementNode.getTextContent()Ljava/lang/String;
去掉xerces.jar或者
构建dom之前加上
System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

可参考:http://www.iteye.com/topic/587404
2.当jaxb与xerces.jar冲突时,
指定jaxb的解析方式,为dom解析,并采用1的设置参数

view plaincopy to clipboardprint?
Marshalling to a javax.xml.transform.SAXResult:   
 
       // assume MyContentHandler instanceof ContentHandler  
       SAXResult result = new SAXResult( new MyContentHandler() );  
 
       m.marshal( element, result );  
      
Marshalling to a javax.xml.transform.DOMResult:   
 
       DOMResult result = new DOMResult();  
         
       m.marshal( element, result );  
      
Marshalling to a javax.xml.transform.StreamResult:   
 
       StreamResult result = new StreamResult( System.out );  
   
       m.marshal( element, result );  
      
Marshalling to a javax.xml.stream.XMLStreamWriter:   
 
       XMLStreamWriter xmlStreamWriter =   
           XMLOutputFactory.newInstance().createXMLStreamWriter( ... );  
   
       m.marshal( element, xmlStreamWriter );  
      
Marshalling to a javax.xml.stream.XMLEventWriter:   
 
       XMLEventWriter xmlEventWriter =   
           XMLOutputFactory.newInstance().createXMLEventWriter( ... );  
   
       m.marshal( element, xmlEventWriter );  
      
 
Unmarshalling from a org.w3c.dom.Node:   
 
       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );  
       Unmarshaller u = jc.createUnmarshaller();  
   
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
       dbf.setNamespaceAware(true);  
       DocumentBuilder db = dbf.newDocumentBuilder();  
       Document doc = db.parse(new File( "nosferatu.xml"));  
 
       Object o = u.unmarshal( doc );  
      
Unmarshalling from a javax.xml.transform.sax.SAXSource using a client specified validating SAX2.0 parser:   
 
       // configure a validating SAX2.0 parser (Xerces2)  
       static final String JAXP_SCHEMA_LANGUAGE =  
           "http://java.sun.com/xml/jaxp/properties/schemaLanguage";  
       static final String JAXP_SCHEMA_LOCATION =  
           "http://java.sun.com/xml/jaxp/properties/schemaSource";  
       static final String W3C_XML_SCHEMA =  
           "http://www.w3.org/2001/XMLSchema";  
 
       System.setProperty( "javax.xml.parsers.SAXParserFactory",  
                           "org.apache.xerces.jaxp.SAXParserFactoryImpl" );  
 
       SAXParserFactory spf = SAXParserFactory.newInstance();  
       spf.setNamespaceAware(true);  
       spf.setValidating(true);  
       SAXParser saxParser = spf.newSAXParser();  
         
       try {  
           saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);  
           saxParser.setProperty(JAXP_SCHEMA_LOCATION, "http://....");  
       } catch (SAXNotRecognizedException x) {  
           // exception handling omitted  
       }  
 
       XMLReader xmlReader = saxParser.getXMLReader();  
       SAXSource source =   
           new SAXSource( xmlReader, new InputSource( "http://..." ) );  
 
       // Setup JAXB to unmarshal  
       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );  
       Unmarshaller u = jc.createUnmarshaller();  
       ValidationEventCollector vec = new ValidationEventCollector();  
       u.setEventHandler( vec );  
         
       // turn off the JAXB provider's default validation mechanism to   
       // avoid duplicate validation  
       u.setValidating( false )  
 
       // unmarshal  
       Object o = u.unmarshal( source );  
 
       // check for events  
       if( vec.hasEvents() ) {  
          // iterate over events  
       }  
      
Unmarshalling from a StAX XMLStreamReader:   
 
       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );  
       Unmarshaller u = jc.createUnmarshaller();  
   
       javax.xml.stream.XMLStreamReader xmlStreamReader =   
           javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... );  
   
       Object o = u.unmarshal( xmlStreamReader );  
      
Unmarshalling from a StAX XMLEventReader:   
 
       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );  
       Unmarshaller u = jc.createUnmarshaller();  
   
       javax.xml.stream.XMLEventReader xmlEventReader =   
           javax.xml.stream.XMLInputFactory().newInstance().createXMLEventReader( ... );  
   
       Object o = u.unmarshal( xmlEventReader );  
     
Marshalling to a javax.xml.transform.SAXResult:

       // assume MyContentHandler instanceof ContentHandler
       SAXResult result = new SAXResult( new MyContentHandler() );

       m.marshal( element, result );
   
Marshalling to a javax.xml.transform.DOMResult:

       DOMResult result = new DOMResult();
      
       m.marshal( element, result );
   
Marshalling to a javax.xml.transform.StreamResult:

       StreamResult result = new StreamResult( System.out );

       m.marshal( element, result );
   
Marshalling to a javax.xml.stream.XMLStreamWriter:

       XMLStreamWriter xmlStreamWriter =
           XMLOutputFactory.newInstance().createXMLStreamWriter( ... );

       m.marshal( element, xmlStreamWriter );
   
Marshalling to a javax.xml.stream.XMLEventWriter:

       XMLEventWriter xmlEventWriter =
           XMLOutputFactory.newInstance().createXMLEventWriter( ... );

       m.marshal( element, xmlEventWriter );
   

Unmarshalling from a org.w3c.dom.Node:

       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
       Unmarshaller u = jc.createUnmarshaller();

       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       dbf.setNamespaceAware(true);
       DocumentBuilder db = dbf.newDocumentBuilder();
       Document doc = db.parse(new File( "nosferatu.xml"));

       Object o = u.unmarshal( doc );
   
Unmarshalling from a javax.xml.transform.sax.SAXSource using a client specified validating SAX2.0 parser:

       // configure a validating SAX2.0 parser (Xerces2)
       static final String JAXP_SCHEMA_LANGUAGE =
           "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
       static final String JAXP_SCHEMA_LOCATION =
           "http://java.sun.com/xml/jaxp/properties/schemaSource";
       static final String W3C_XML_SCHEMA =
           "http://www.w3.org/2001/XMLSchema";

       System.setProperty( "javax.xml.parsers.SAXParserFactory",
                           "org.apache.xerces.jaxp.SAXParserFactoryImpl" );

       SAXParserFactory spf = SAXParserFactory.newInstance();
       spf.setNamespaceAware(true);
       spf.setValidating(true);
       SAXParser saxParser = spf.newSAXParser();
      
       try {
           saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
           saxParser.setProperty(JAXP_SCHEMA_LOCATION, "http://....");
       } catch (SAXNotRecognizedException x) {
           // exception handling omitted
       }

       XMLReader xmlReader = saxParser.getXMLReader();
       SAXSource source =
           new SAXSource( xmlReader, new InputSource( "http://..." ) );

       // Setup JAXB to unmarshal
       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
       Unmarshaller u = jc.createUnmarshaller();
       ValidationEventCollector vec = new ValidationEventCollector();
       u.setEventHandler( vec );
      
       // turn off the JAXB provider's default validation mechanism to
       // avoid duplicate validation
       u.setValidating( false )

       // unmarshal
       Object o = u.unmarshal( source );

       // check for events
       if( vec.hasEvents() ) {
          // iterate over events
       }
   
Unmarshalling from a StAX XMLStreamReader:

       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
       Unmarshaller u = jc.createUnmarshaller();

       javax.xml.stream.XMLStreamReader xmlStreamReader =
           javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... );

       Object o = u.unmarshal( xmlStreamReader );
   
Unmarshalling from a StAX XMLEventReader:

       JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
       Unmarshaller u = jc.createUnmarshaller();

       javax.xml.stream.XMLEventReader xmlEventReader =
           javax.xml.stream.XMLInputFactory().newInstance().createXMLEventReader( ... );

       Object o = u.unmarshal( xmlEventReader );
   




可参考:http://www.coderanch.com/t/495263/Web-Services/java/type-marshalling-does-JAXB

你可能感兴趣的:(JAXB)