Monday, November 17, 2008

How to parse a xml without the dtd-validating

Thanks to Samuel now my testXMLReader.java can read XML files without validating the dtd files referred in the second line of each XML files. Here is the code.


package org.tingting.bn.mn.phylotree.test;

import java.io.File;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import org.tingting.bn.mn.graph.impl.jaxp.Pathway;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;

/**
* TestPathwayReader.java is to test PathwayReader
*
* @author Tingting
*
*/

public class TestPathwayReader {

/**
* @param args
*/
public static void main(String[] args) {


String xmlFile = "D:\\Data\\KEGG\\KGML\\aae\\aae00020.xml";

try {
// parse the xml files.
JAXBContext jc = JAXBContext
.newInstance("org.tingting.bn.mn.graph.impl.jaxp");
Unmarshaller u = jc.createUnmarshaller();

Pathway pathway = u.unmarshal(getSAXSource(new File(xmlFile)),
Pathway.class).getValue();

System.out.println(pathway.getName());
} catch (Exception e) {
System.out.println(e);
}

}

// this function helps unmarshal avoid the DTD-validating.
private static SAXSource getSAXSource(File suiteFile)
throws SAXNotSupportedException, SAXException,
ParserConfigurationException, FileNotFoundException {


/*
// These two lines needs the support of xerces
System.setProperty("javax.xml.parsers.SAXParserFactory",
"org.apache.xerces.jaxp.SAXParserFactoryImpl");
*/

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(false);

// System.out.println(spf.isValidating());
SAXParser saxParser = spf.newSAXParser();

// System.out.println(saxParser.isValidating());
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setFeature("http://xml.org/sax/features/validation", false);
xmlReader
.setFeature(
"http://apache.org/xml/features/nonvalidating/load-external-dtd",
false);
SAXSource source = new SAXSource(xmlReader, new InputSource(suiteFile
.getPath()));

return source;
}

}

No comments: