gpt4 book ai didi

java - SAXParseException 为 getSystemId() 返回 null

转载 作者:行者123 更新时间:2023-11-29 08:14:18 24 4
gpt4 key购买 nike

为什么 SAXParseException 对 getSystemId() 返回 null?什么是系统标识符?

    import java.io.StringReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;

public class MainClass {
static public void main(String[] arg) throws Exception{
boolean validate = false;

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(validate);

XMLReader reader = null;
SAXParser parser = spf.newSAXParser();
reader = parser.getXMLReader();

reader.setErrorHandler(new MyErrorHandler());
reader.parse(new InputSource(new StringReader(xmlString)));
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME>Joe Wang</NAME>" +
" <EMAIL><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7bdb8b297aeb8a2a5a4b2a5a1b2a5f9b4b8ba" rel="noreferrer noopener nofollow">[email protected]</a></EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.java2s.com</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAE>" + // error here
" <EMAIL><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="076c6675686b477e6872757462757162752964686a" rel="noreferrer noopener nofollow">[email protected]</a></EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.java2s.com</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbbaeb9b9b29ca5b3a9aeafb9aeaab9aef2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a></EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.java2s.com</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
show("Warning", e);
throw (e);
}

public void error(SAXParseException e) throws SAXException {
show("Error", e);
throw (e);
}

public void fatalError(SAXParseException e) throws SAXException {
show("Fatal Error", e);
throw (e);
}

private void show(String type, SAXParseException e) {
System.out.println(type + ": " + e.getMessage());
System.out.println("Line " + e.getLineNumber() + " Column "
+ e.getColumnNumber());
System.out.println("System ID: " + e.getSystemId());
System.out.println(e);

}
}

最佳答案

XML 中的“系统标识符”是您从中获取某些内容的物理位置。当您只是解析内存中的字符串时,它根本没有系统标识符,除非您进行额外的调用来给它一个系统标识符。

在这种情况下,您可以调用InputSource.setSystemId

关于java - SAXParseException 为 getSystemId() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5708286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com