Created
December 12, 2012 06:13
-
-
Save omnisis/4265387 to your computer and use it in GitHub Desktop.
DOM4J: configure sax reader to remove ns uris
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cliff.xmlns; | |
import org.dom4j.Document; | |
import org.dom4j.DocumentFactory; | |
import org.dom4j.Node; | |
import org.dom4j.XPath; | |
import org.dom4j.io.SAXReader; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.xml.sax.Attributes; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.XMLReader; | |
import org.xml.sax.helpers.XMLFilterImpl; | |
import org.xml.sax.helpers.XMLReaderFactory; | |
import java.io.File; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.fail; | |
public class DOM4JXmlNsTest { | |
private SAXReader saxReader; | |
private Document document; | |
private class NamespaceStrippingFilter extends XMLFilterImpl { | |
@Override | |
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { | |
super.startElement(null, localName, localName, attributes); | |
} | |
@Override | |
public void endElement(String uri, String localName, String qName) throws SAXException { | |
super.endElement(null, localName, localName); | |
} | |
} | |
@Before | |
public void setup() throws Exception { | |
XMLReader saxXmlReader; | |
try { | |
saxXmlReader = XMLReaderFactory.createXMLReader( | |
"org.apache.xerces.parsers.SAXParser" | |
); | |
System.out.println("Successfully found Xerces parser!"); | |
} | |
catch (SAXException e) { | |
try { | |
saxXmlReader = XMLReaderFactory.createXMLReader(); | |
System.out.println("Falling back to default SAX Reader: " + saxXmlReader.getClass().getName()); | |
} | |
catch (SAXException e2) { | |
throw new NoClassDefFoundError("No SAX parser is available"); | |
// or whatever exception your method is declared to throw | |
} | |
} | |
NamespaceStrippingFilter filter = new NamespaceStrippingFilter(); | |
filter.setParent(saxXmlReader); | |
//DocumentFactory.getInstance().setXPathNamespaceURIs(nsMap); | |
saxReader = new SAXReader(saxXmlReader, false); | |
saxReader.setXMLFilter(filter); | |
} | |
public Document readDoc(String path) { | |
try { | |
return saxReader.read(new File(path)); | |
} catch(Exception ex) { | |
fail("Caught exception trying to read: "+path); | |
} | |
return null; | |
} | |
@Test | |
public void testNSXml() { | |
document = readDoc("src/test/resources/example1.xml"); | |
assertNotNull(document); | |
Node n = document.selectSingleNode("html/body/bookreview"); | |
System.out.println(n.asXML()); | |
} | |
@Test | |
public void testNoNSXml() { | |
document = readDoc("src/test/resources/example1-overriden-rootns.xml"); | |
assertNotNull(document); | |
Node n = document.selectSingleNode("html/body/bookreview"); | |
System.out.println(n.asXML()); | |
} | |
@Test | |
public void perfTest() { | |
XPath compiledXpath = DocumentFactory.getInstance().createXPath("html/body/bookreview"); | |
int iterations = 3000; | |
long start = System.currentTimeMillis(); | |
Node n = null; | |
for(int i=0; i<iterations; i++) { | |
document = readDoc("src/test/resources/example1.xml"); | |
//n = compiledXpath.selectSingleNode(document); | |
//if (n == null) | |
// fail("Xpath selected no nodes!"); | |
//System.out.println(n.asXML()); | |
} | |
long elapsedTime = System.currentTimeMillis()-start; | |
double avgTime = elapsedTime/iterations; | |
System.out.println(String.format("%d iterations in %d ms, avgTime: %5.2f ms, docsPerSec: %5.2f", | |
iterations, | |
elapsedTime, | |
avgTime, | |
1000/avgTime)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment