Reputation: 8366
I am writing a java code to validate XMLs against XSD file. Eclipse shows 2 error in following code.
Multiple Markers at this line -
URL cannot be resolved to a type
SAXException cannot be resolved to a type
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 link for below program :
Upvotes: 2
Views: 38072
Reputation: 1
Copy and paste 'URL' class somewhere in your machine with extension of .Java, delete URL class from package, and copy URL Class paste on package folder, Clean the projects. it's worked for me.
Upvotes: -1
Reputation: 42005
This error occurs because you have used some classes from some other package and Compiler is not able to resolve those dependencies because of missing imports.
Use Ctrl+Shift+O to Auto import all the required dependencies. Or use manual import as suggested by @Reimeus.
Upvotes: 3
Reputation: 159864
Import the missing classes so that the unqualified types can be used in the program
import java.net.URL;
import org.xml.sax.SAXException;
Upvotes: 9