Moving stuff from school pc to home
This commit is contained in:
@@ -1,9 +1,26 @@
|
||||
package shared.xml;
|
||||
|
||||
import mock.xml.Race;
|
||||
import mock.xml.RaceXMLCreator;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.validation.Schema;
|
||||
import javax.xml.validation.SchemaFactory;
|
||||
import javax.xml.validation.Validator;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 13/08/17.
|
||||
@@ -21,16 +38,49 @@ public class XMLUtilities {
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
public static String xmlToClass(){
|
||||
// JAXBContext jc = JAXBContext.newInstance(YourXMLClass.class);
|
||||
// Unmarshaller unmarshaller = jc.createUnmarshaller();
|
||||
//
|
||||
// SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
// URL schemaURL = // The URL to your XML Schema;
|
||||
// Schema schema = sf.newSchema(schemaURL);
|
||||
// unmarshaller.setSchema(schema);
|
||||
//
|
||||
// YourXMLClass yourXMLClass = (YourXMLClass) unmarshaller.unmarshal(xml);
|
||||
public static Object xmlToClass(File file, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = parser.parse(file);
|
||||
|
||||
return xmlToClass(document, schemaURL, c);
|
||||
}
|
||||
|
||||
public static Object xmlToClass(String xml, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = parser.parse(xml);
|
||||
|
||||
return xmlToClass(document, schemaURL, c);
|
||||
}
|
||||
|
||||
public static Object xmlToClass(Document document, URL schemaURL, Class c) throws ParserConfigurationException, IOException, SAXException, JAXBException {
|
||||
JAXBContext jc = JAXBContext.newInstance(c);
|
||||
Unmarshaller unmarshaller = jc.createUnmarshaller();
|
||||
|
||||
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Schema schema = sf.newSchema(schemaURL);
|
||||
unmarshaller.setSchema(schema);
|
||||
|
||||
return unmarshaller.unmarshal(new DOMSource(document));
|
||||
}
|
||||
|
||||
public static boolean validateXML(String file, URL schemaURL){
|
||||
try {
|
||||
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = parser.parse(new File(file));
|
||||
|
||||
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Schema schema = sf.newSchema(schemaURL);
|
||||
Validator validator = schema.newValidator();
|
||||
validator.validate(new DOMSource(document));
|
||||
} catch (ParserConfigurationException e) {
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
} catch (SAXException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user