
net.sf.xolite.company.XMLSerializableExample Maven / Gradle / Ivy
/*-------------------------------------------------------------------------
Copyright 2006 Olivier Berlanger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------------------------------------*/
package net.sf.xolite.company;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import net.sf.xolite.XMLParseException;
import net.sf.xolite.sax.SaxXMLEventParser;
import net.sf.xolite.utils.ConsoleXMLSerializer;
import net.sf.xolite.utils.SchemaLocation;
import net.sf.xolite.utils.StreamXMLSerializer;
import org.apache.log4j.xml.DOMConfigurator;
import org.xml.sax.InputSource;
public class XMLSerializableExample {
public void run(boolean useSchema) throws Exception {
// setup the parser
SaxXMLEventParser parser = (useSchema) ? new SaxXMLEventParser(getSchemaLocation()) : new SaxXMLEventParser();
// get the input stream
String resourceName = "Company.xml";
InputStream is = getClass().getResourceAsStream(resourceName);
if (is == null) throw new XMLParseException("Resource '" + resourceName + "' not found");
// parse the company
Company cmp = new Company();
parser.parse(new InputSource(is), cmp);
// display result
System.out.println("Loaded: " + cmp);
System.out.println("");
// serialize back in the console
ConsoleXMLSerializer.dump(cmp);
// Also write it in a temporary file rather than console
File outputFile = File.createTempFile("companyExample_", ".xml");
OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
StreamXMLSerializer serializer = new StreamXMLSerializer(out);
serializer.startPrefixMapping("", Company.COMPANY_NAMESPACE_URI); // define the Company namespace as the default one
serializer.serializeObject(cmp);
System.out.println("Written in temporary file: " + outputFile.getAbsolutePath());
}
/**
* The schema location is the schema URI followed the URL pointing to the actual schema XML definition file (separated by a
* space). Its exactly like the value of the 'xsi:schemaLocation' XML attribute.
*/
public String getSchemaLocation() {
return SchemaLocation.getResourceLocation(Company.COMPANY_NAMESPACE_URI, getClass(), "Company.xsd");
}
/**
* Configure log4j using the "log4j.xml" file from the default directory.
*/
public static void setupLogging() {
try {
DOMConfigurator.configure(new File("log4j.xml").toURI().toURL());
} catch (MalformedURLException mue) {
throw new IllegalStateException("Invalid log4j configuration file URL", mue);
}
}
public static void main(String[] args) throws Exception {
setupLogging();
new XMLSerializableExample().run(true);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy