我要执行下面的代码,但我的SAX解析器没有调用startElement方法。
以下是我的代码:
package getTableStructure;
import java.util.List;
import java.io.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import getTableStructure.DBdetails;
public class InformaticaObjectParser {
//H:/Eclipse_Workspace/GWM_SHARED_DEFINITION.XML
//H:/Eclipse_Workspace/InformaticaTableStruct/
public static void main(String[] args) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
if (args.length == 2){
System.out.println(args[1]);
File opDir = new File(args[1].trim());
if (opDir.exists() && opDir.isDirectory()){
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
System.out.println("Parser object created");
MyHandler handler = new MyHandler();
System.out.println(handler);
saxParser.parse(new File(args[0]), handler);
String outputDir = args[1].trim();
System.out.println("I am here");
List<DBdetails> TableList = handler.getEmpList();
for(DBdetails db : TableList){
System.out.println("Table list from XML: " + TableList);
//BufferedWriter out = new BufferedWriter(new FileWriter("H:/Eclipse_Workspace/TableStruct/" + tableName + ".csv"));
BufferedWriter out = new BufferedWriter(new FileWriter( outputDir + db.getName().trim()+"."+db.getSource().trim()+".csv"));
for (String k : db.dbdetail.keySet() ){
out.write(k+","+db.dbdetail.get(k));
out.newLine();
}
out.close();
}
}
catch (ParserConfigurationException | SAXException | IOException e) {
e.getMessage();
}
}
else{
System.out.println("2 arguments");
}
}
else{
System.out.println(" Number of arguments should be 2");
}
}
}
MY Handler:
package getTableStructure;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import getTableStructure.DBdetails;
public class MyHandler extends DefaultHandler {
private List<DBdetails> sourceList = null;
private DBdetails table = null;
public List<DBdetails> getEmpList() {
return sourceList;
}
boolean bSource = false;
boolean bName = false;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("In MyHandler");
System.out.println(qName);
if (qName.equalsIgnoreCase("SOURCE")) {
table = new DBdetails();
String NAME = attributes.getValue("DBDNAME");
String DbName = attributes.getValue("NAME");
table.setName(NAME.trim());
table.setSource(DbName.trim());
if (sourceList == null)
sourceList = new ArrayList<>();
} else if (qName.equalsIgnoreCase("DBDNAME")) {
bName = true;
} else if (qName.equalsIgnoreCase("NAME")) {
bSource = true;
} else if (qName.equalsIgnoreCase("SOURCEFIELD")) {
String details = (attributes.getValue("DATATYPE").trim()+","+attributes.getValue("PRECISION").trim()+","+attributes.getValue("SCALE").trim());
this.table.dbdetail.put(attributes.getValue("NAME").trim(),
details);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("SOURCE")) {
sourceList.add(table);
}
}
@Override
public void characters(char ch[], int start, int length)
throws SAXException {
if (bName) {
table.setName(new String(ch, start, length));
bName = false;
} else if (bSource) {
table.setSource(new String(ch, start, length));
bSource = false;
}
}
}
以下是XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
<POWERMART CREATION_DATE="06/01/2015 11:44:43" REPOSITORY_VERSION="182.91">
<REPOSITORY NAME="PB_DEV02" VERSION="182" CODEPAGE="Latin1" DATABASETYPE="Sybase">
<FOLDER NAME="PB_DEFINITIONS" GROUP="" OWNER="E466701" SHARED="SHARED" DESCRIPTION="Place all PB source/target definitions here and use shortcuts for them in other PB_* folders." PERMISSIONS="rwx---rwx" UUID="6c90f1e5-b619-48fd-8ee1-5b0b136947ea">
<SOURCE BUSINESSNAME ="" DATABASETYPE ="Sybase" DBDNAME ="database" DESCRIPTION ="" NAME ="table_name" OBJECTVERSION ="1" OWNERNAME ="dbo" VERSIONNUMBER ="1">
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" DESCRIPTION ="" FIELDNUMBER ="1" FIELDPROPERTY ="0" FIELDTYPE ="ELEMITEM" HIDDEN ="NO" KEYTYPE ="PRIMARY KEY" LENGTH ="0" LEVEL ="0" NAME ="Col1" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="0" PHYSICALLENGTH ="8" PHYSICALOFFSET ="0" PICTURETEXT ="" PRECISION ="8" SCALE ="0" USAGE_FLAGS =""/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" DESCRIPTION ="" FIELDNUMBER ="2" FIELDPROPERTY ="0" FIELDTYPE ="ELEMITEM" HIDDEN ="NO" KEYTYPE ="NOT A KEY" LENGTH ="0" LEVEL ="0" NAME ="col2" NULLABLE ="NULL" OCCURS ="0" OFFSET ="0" PHYSICALLENGTH ="254" PHYSICALOFFSET ="8" PICTURETEXT ="" PRECISION ="254" SCALE ="0" USAGE_FLAGS =""/>
</SOURCE>
..
..
..
</FOLDER>
</REPOSITORY>
</POWERMART>
几个月前我写这个项目的时候工作完美但当我现在打开它并试图运行 InformaticaObjectParser 时,它没有在 MyHandler <中调用start元素方法/ strong>并且编程存在而不会出现任何错误。
如果您需要任何其他信息,请与我们联系。
由于
答案 0 :(得分:1)
尝试覆盖fatalError,错误,警告方法以查看文档中是否存在错误
public class MyHandler extends DefaultHandler {
...
@Override
public void fatalError(SAXParseException e) throws SAXException {
LOG.log(Level.SEVERE, e.getMessage(), e);
throw new SAXException(e.getMessage());
}
@Override
public void error(SAXParseException e) throws SAXException {
LOG.log(Level.SEVERE, e.getMessage(), e);
throw new SAXException(e.getMessage());
}
@Override
public void warning(SAXParseException e) throws SAXException {
LOG.log(Level.WARNING, e.getMessage(), e);
}
...
}
答案 1 :(得分:1)
您可以在不从xml中删除DOCTYPE的情况下使其正常工作。只需添加将dtd解析为空文档的实体解析器,如下所示:
saxParser.getXMLReader().setEntityResolver(new EntityResolver(){
public InputSource resolveEntity(String publicId,String systemId){
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
答案 2 :(得分:0)
<!DOCTYPE POWERMART SYSTEM "powrmart.dtd">
当我删除它时,代码有效。我不确定它的原因。有人可以对此有所了解吗?
再次感谢您的时间。