sách gpt4 ai đã đi

Trình phân tích cú pháp SAX không gọi phần tử bắt đầu

In lại 作者:行者123 更新时间:2023-12-02 04:37:18 27 4
mua khóa gpt4 Nike

我尝试执行下面的代码,但我的 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()){
thử {
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 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();
}
ra ngoài.đóng();
}
}
catch (ParserConfigurationException | SAXException | IOException e) {
e.getMessage();
}
}
else{
System.out.println("2 arguments");
}
}
else{
System.out.println(" Number of arguments should be 2");
}
}
}

我的处理程序:

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 sourceList = null;
private DBdetails table = null;
public List getEmpList() {
return sourceList;
}
boolean bSource = false;
boolean bName = false;

@Ghi đè
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);
}
}

@Ghi đè
public void endElement(String uri, String localName, String qName)
ném SAXException {
if (qName.equalsIgnoreCase("SOURCE")) {
sourceList.add(table);
}
}

@Ghi đè
public void characters(char ch[], int start, int length)
ném 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:










..
..
..



几个月前,当我编写这个项目时,它工作得很好,但是当我现在打开它并尝试运行 InformaticaObjectParser 时,它没有调用 MyHandler< 中的 start 元素方法/strong> 并且编程存在且没有给出任何错误。

如果您需要任何其他信息,请告诉我。

Cảm ơn

1 Câu trả lời

尝试重写 fatalError、error、warning 方法来查看文档中是否存在错误

public class MyHandler extends DefaultHandler {
...

@Ghi đè
public void fatalError(SAXParseException e) throws SAXException {
LOG.log(Level.SEVERE, e.getMessage(), e);
throw new SAXException(e.getMessage());
}

@Ghi đè
public void error(SAXParseException e) throws SAXException {
LOG.log(Level.SEVERE, e.getMessage(), e);
throw new SAXException(e.getMessage());
}

@Ghi đè
public void warning(SAXParseException e) throws SAXException {
LOG.log(Level.WARNING, e.getMessage(), e);
}
...
}

关于java - SAX 解析器不调用开始元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30603696/

27 4 0
Bài viết được đề xuất: java - if else block 没有打印石头剪刀布游戏的语句
Bài viết được đề xuất: java - 我怎样才能让一行代码运行一次?
Bài viết được đề xuất: Java AEM 查询生成器 ||
Bài viết được đề xuất: java - 图像旋转方法在内存中留下一些东西
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com