/** * Constructor allowing DTD validation of documents. * * @param resolver An entity resolver to use if validation is enabled. * @see #setValidating(boolean) */ public XmlParser(EntityResolver resolver) { this.entityResolver = resolver; result = new DefaultParseResult(this); try { spf = SAXParserFactory.newInstance(); } catch (FactoryConfigurationError fce) { fce.printStackTrace(); } }
public final void testRandom() { final XMLSnippets snips = new XMLSnippets(); Document theD = null; try { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); theD = builder.newDocument(); } catch (FactoryConfigurationError factoryConfigurationError) { factoryConfigurationError.printStackTrace(); // To change body of catch // statement use Options | // File Templates. } catch (ParserConfigurationException e) { e.printStackTrace(); // To change body of catch statement use Options | // File Templates. } // check it worked assertNotNull("created document", theD); for (int i = 0; i < 4; i++) { if (theD != null) { final Element thisE = theD.createElement("a" + (i + 1)); snips._mySnippets.add(thisE); } } int num1 = 0; int num2 = 0; int num3 = 0; int num4 = 0; final int len = 10000; for (int i = 0; i < len; i++) { final Element newE = snips.getInstance(); final String nm = newE.getNodeName(); final int val = Integer.parseInt(new String("" + nm.charAt(1))); if (val == 1) { num1++; } if (val == 2) { num2++; } if (val == 3) { num3++; } if (val == 4) { num4++; } } assertEquals("correct 1s", num1, 2500, 200); assertEquals("correct 2s", num2, 2500, 200); assertEquals("correct 3s", num3, 2500, 200); assertEquals("correct 4s", num4, 2500, 200); }
/** * Helper function. It reads an XML file and returns the in-memory representation * * <p>It accepts only valid documents * * @param file * @return Returns the in-memory XML representation of the stored XML file * @throws FactoryConfigurationError */ public static Document getXMLFromFile(String file) throws FactoryConfigurationError { Document MIQuery = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // factory.setValidating(true); // Amazon does not put a DTD factory.setValidating(false); factory.setNamespaceAware(true); factory.setCoalescing(true); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler( new org.xml.sax.ErrorHandler() { // ignore fatal errors (an exception is guaranteed) public void fatalError(SAXParseException exception) { System.out.println( "** Error" + ", line " + exception.getLineNumber() + ", uri " + exception.getSystemId()); System.out.println(" " + exception.getMessage()); } // treat validation errors as fatal public void error(SAXParseException e) throws SAXParseException { System.out.println( "** Error" + ", line " + e.getLineNumber() + ", uri " + e.getSystemId()); System.out.println(" " + e.getMessage()); throw e; } // dump warnings too public void warning(SAXParseException err) { System.out.println( "** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } }); InputSource inputSource = new InputSource(new StringReader(file)); MIQuery = builder.parse(inputSource); } catch (SAXException sxe) { // Error generated during parsing Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException | IOException pce) { // Parser with specified options can't be built pce.printStackTrace(); } catch (FactoryConfigurationError fce) { // Factory configuration error fce.printStackTrace(); } return MIQuery; }