具有 IBM Websphere 依赖性的 SAXParser 实现
SAXParser implementation with IBM Websphere dependency
我正在尝试从 S.O 周围的 Java 专家那里获得一些帮助。关于这个问题。我在一个长期项目中遇到了 XMLParser 的旧实现......
在我看来这个实现是不正确的,或者可以改进..
我想知道是否有人可以指导该怎么做,我们将不胜感激...
这是一个具有 pom.xml 依赖关系的 Maven 项目...
问题...
所以基本上有人在项目中使用了直接来自 IBM 内部 JRE 的 class SAXParser...
我怎样才能将这种安静的代码转换为不依赖于 WAS (Websphere Aplication Server) 的依赖项?
public boolean parse(){
boolean res = false;
try {
SAXParser p = new SAXParser(); // Need to replace this for better aproach
p.setContentHandler(this); // Need to replace this for better aproach
InputSource inputSource = new InputSource(new StringReader(source));
if (inputSource != null){
p.parse(inputSource); // Need to replace this for better aproach
}
res = true;
}
catch (Exception e) {
System.err.println("public void parse()"+e.getLocalizedMessage());
res= false;
e.printStackTrace();
}
return res;
}
更新
迁移成功:)
...
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
...
public boolean parse(){
boolean res = false;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
javax.xml.parsers.SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(this);
InputSource inputSource = new InputSource(new StringReader(source));
if (inputSource != null){
xmlReader.parse(inputSource);
}
res = true;
}
catch (Exception e) {
System.err.println("public void parse()"+e.getLocalizedMessage());
res= false;
e.printStackTrace();
}
return res;
}
您可以从 javax.xml.parsers.SAXParserFactory:
请求 javax.xml.parsers.SAXParser 而不是显式构建 SAXParser 实现
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
These lines of code create a SAXParserFactory instance, as determined by the setting of the javax.xml.parsers.SAXParserFactory system property.
本文来自 the Java tutorial for Parsing an XML file using SAX.
然后可以从SAXParser获取XMLReader,设置contentHandler,从InputSource解析xml:
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new WhateverYouNameYourContentHandler());
xmlReader.parse(inputSource);
我正在尝试从 S.O 周围的 Java 专家那里获得一些帮助。关于这个问题。我在一个长期项目中遇到了 XMLParser 的旧实现...... 在我看来这个实现是不正确的,或者可以改进.. 我想知道是否有人可以指导该怎么做,我们将不胜感激...
这是一个具有 pom.xml 依赖关系的 Maven 项目...
问题...
我怎样才能将这种安静的代码转换为不依赖于 WAS (Websphere Aplication Server) 的依赖项?
public boolean parse(){
boolean res = false;
try {
SAXParser p = new SAXParser(); // Need to replace this for better aproach
p.setContentHandler(this); // Need to replace this for better aproach
InputSource inputSource = new InputSource(new StringReader(source));
if (inputSource != null){
p.parse(inputSource); // Need to replace this for better aproach
}
res = true;
}
catch (Exception e) {
System.err.println("public void parse()"+e.getLocalizedMessage());
res= false;
e.printStackTrace();
}
return res;
}
更新
迁移成功:)
...
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
...
public boolean parse(){
boolean res = false;
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
javax.xml.parsers.SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(this);
InputSource inputSource = new InputSource(new StringReader(source));
if (inputSource != null){
xmlReader.parse(inputSource);
}
res = true;
}
catch (Exception e) {
System.err.println("public void parse()"+e.getLocalizedMessage());
res= false;
e.printStackTrace();
}
return res;
}
您可以从 javax.xml.parsers.SAXParserFactory:
请求 javax.xml.parsers.SAXParser 而不是显式构建 SAXParser 实现SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); SAXParser saxParser = spf.newSAXParser();
These lines of code create a SAXParserFactory instance, as determined by the setting of the javax.xml.parsers.SAXParserFactory system property.
本文来自 the Java tutorial for Parsing an XML file using SAX.
然后可以从SAXParser获取XMLReader,设置contentHandler,从InputSource解析xml:
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new WhateverYouNameYourContentHandler());
xmlReader.parse(inputSource);