`

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

    博客分类:
  • java
 
阅读更多
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
分享到:
评论

相关推荐

    dom4j-1.6.1.jar jdom.jar xercesImpl.jar 三种包下载

    JDOM的1.x系列版本(如提供的jdom.jar)适用于许多旧项目,但在现代项目中,可能已被更新的API如DOM4J或Java 7引入的Java/XML绑定工具如JAXB所取代。 3. XercesImpl.jar Xerces是Apache软件基金会开发的一个开源XML...

    xml解析Demo以及相关jar包

    - **xercesImpl**:Apache Xerces是一个开源的XML解析器,提供了DOM、SAX和XSLT的实现。 - **xml-apis**:XML API接口,包含SAX、DOM和其他XML相关接口。 4. **XML解析流程** - 读取XML文件:使用BufferedReader...

    java读取xml用到的jar包集合

    Xerces是Apache软件基金会开发的一个开源XML解析器,它可以作为DOM、SAX和XML Schema处理器。Xerces提供了高度可配置的解析选项,对于需要深度定制XML解析的项目非常有用。 8. **JAXB RI (Runtime Implementation)...

    axis2完整jar

    8. **xercesImpl-2.8.1.jar**:Xerces是Apache的一个XML解析器项目,它实现了XML 1.0和1.1规范,以及DOM、SAX和XML Schema接口。版本2.8.1是这个解析器的一个版本。 9. **jaxb-impl-2.1.6.jar**:这是JAXB实现的主...

    xmlbeans的jar包

    2. **resolver.jar**:这个文件通常包含XML解析器的依赖,如Apache的Xerces解析器,用于解析XML文档并处理DTD(文档类型定义)和XML Schema。 3. **xbean.jar**:这是XMLBeans的核心库,它实现了XML Schema到Java类...

    XML的jar包

    在Java项目中,常见的XML处理库有Apache的Xerces和Woodstox,它们提供了高效的XML解析器和实现。此外,Spring框架也内置了对XML配置的支持,使得开发者可以使用XML来定义bean和配置。 在使用XML的jar包时,需要注意...

    xfire 运行所需jar

    压缩包中的JAR文件涵盖了各种依赖,例如XML解析器(如Xerces或DOM4J)、HTTP客户端库(如Commons HttpClient)、 logging框架(如Log4j)、以及一些通用工具类库(如Commons Lang、Commons Collections)。...

    Java中org.apache.crimson.tree开发包

    然而,尽管Apache Crimson曾经是一个流行的XML解析库,但它现在已经被更现代、更高效的库如Java自带的JAXB(Java Architecture for XML Binding)和Apache Xerces所取代。这些现代库提供了更好的性能和更多的XML标准...

    JAVA操作XML文件

    - Xerces是Apache软件基金会开发的一个开源XML解析器,提供了对DOM、SAX和XSLT的支持,`xercesImpl.jar`是这个解析器的实现。 5. **JSP和Servlet**: - `servlet-api.jar`和`jsp-api.jar`是Java服务器页面(JSP)...

    web service开发包

    5. **xercesImpl-2.6.2.jar**:Xerces是Apache的一个XML解析器实现,它实现了XML 1.0和1.1规范,以及相关的DOM、SAX和XML Schema接口。这个库对于处理XML文档至关重要。 6. **xfire-all-1.2.6.jar**:XFire是早期的...

    axis2-1.5.2-bin.zip lib

    5. **xercesImpl-2.8.1.jar**:Xerces是Apache的XML解析器实现,它解析XML文档并将其转换为DOM(Document Object Model)或其他形式的数据结构。 6. **jaxb-impl-2.1.7.jar**:这是JAXB的实现库,用于处理XML和Java...

    xml-commons-external-1.4.01-bin.zip

    Xerces2 Java是其第二代产品,提供DOM(文档对象模型)、SAX(简单API for XML)和XP(XPath)接口,支持XML Schema、DTD(文档类型定义)以及命名空间。 2. **Apache XML APIs**: 这是一组与XML相关的API集合,...

    ssh lib包2

    10. **xercesImpl-2.6.2.jar**:Xerces是Apache的一个XML解析器,实现了W3C的DOM、SAX和XML Schema规范,用于解析和验证XML文档。 这些库组合在一起,可以构建出一个功能强大的Java应用,包括SSH连接、数据库操作、...

Global site tag (gtag.js) - Google Analytics