在Groovy中使用stax解析器

时间:2017-06-13 11:33:56

标签: java groovy xml-parsing stax

我试图让stax解析器在Groovy中运行。但我得到以下例外。

Caught: javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found
javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found
at com.charan.xml.parser.XmlParserDelete.run(XmlParserDelete.groovy:15)

我正在使用的代码:

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stax.StAXSource;
//class XmlParserDelete {
//
//}

XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(args[0]));
xsr.nextTag(); // Advance to statements element

TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
    DOMResult result = new DOMResult();
    t.transform(new StAXSource(xsr), result);
    Node domNode = result.getNode();
}

尝试使用 stax-1.2.0.jar 作为依赖项。但是得到以下错误。

Caught: java.lang.ClassCastException: com.bea.xml.stream.MXParserFactory cannot be cast to javax.xml.stream.XMLInputFactory
java.lang.ClassCastException: com.bea.xml.stream.MXParserFactory cannot be cast to javax.xml.stream.XMLInputFactory
at com.charan.xml.parser.XmlParserDelete.run(XmlParserDelete.groovy:15)

有没有人尝试过使用Stov和Groovy?请帮我解决这个问题。

0 个答案:

没有答案