Java eclipse 错误 - 此行有多个标记 - URL 和 SAXException 无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20997592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java eclipse error - Multiple Markers at this line - URL & SAXException cannot be resolved to a type
提问by logan
I am writing a java code to validate XMLs against XSD file. Eclipse shows 2 error in following code.
我正在编写一个 Java 代码来根据 XSD 文件验证 XML。Eclipse 在以下代码中显示 2 个错误。
Multiple Markers at this line -
URL cannot be resolved to a type
SAXException cannot be resolved to a type
此行有多个标记 -
URL 无法解析为类型
SAXException 无法解析为类型
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
public class xml_validator_class {
public static void main(String argv[]) {
URL schemaFile = new URL("xsdfile.xsd");
Source xmlFile = new StreamSource(new File("xmlfile.xml"));
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
try {
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid");
System.out.println("Reason: " + e.getLocalizedMessage());
}
}
}
Kindly refer to this linkfor below program :
请参阅此链接以了解以下程序:
采纳答案by Reimeus
Import the missing classes so that the unqualified types can be used in the program
导入缺少的类,以便程序中可以使用不合格的类型
import java.net.URL;
import org.xml.sax.SAXException;
回答by Narendra Pathai
This error occurs because you have used some classes from some other package and Compiler is not able to resolve those dependenciesbecause of missing imports.
出现此错误是因为您使用了其他包中的某些类,并且 Compiler由于缺少导入而无法解析这些依赖项。
Use Ctrl+Shift+Oto Auto import all the required dependencies. Or use manual import as suggested by @Reimeus.
使用Ctrl+ Shift+O为自动导入所有需要的依赖。或者按照@Reimeus 的建议使用手动导入。