本文整理汇总了Java中javax.xml.parsers.DocumentBuilder.setErrorHandler方法的典型用法代码示例。如果您正苦于以下问题:Java DocumentBuilder.setErrorHandler方法的具体用法?Java DocumentBuilder.setErrorHandler怎么用?Java DocumentBuilder.setErrorHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.parsers.DocumentBuilder
的用法示例。
在下文中一共展示了DocumentBuilder.setErrorHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
public static Flag parse(String name, InputStream in) throws IOException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true); // make sure the XML is valid
factory.setExpandEntityReferences(false); // don't allow custom entities
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new KVXXEntityResolver());
builder.setErrorHandler(new KVXXErrorHandler(name));
Document document = builder.parse(new InputSource(in));
return parseDocument(document);
} catch (ParserConfigurationException pce) {
throw new IOException(pce);
} catch (SAXException saxe) {
throw new IOException(saxe);
}
}
示例2: testCheckElementContentWhitespace
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* Test for the isIgnoringElementContentWhitespace and the
* setIgnoringElementContentWhitespace. The xml file has all kinds of
* whitespace,tab and newline characters, it uses the MyNSContentHandler
* which does not invoke the characters callback when this
* setIgnoringElementContentWhitespace is set to true.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckElementContentWhitespace() throws Exception {
String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
String outputFile = USER_DIR + "dbfactory02.out";
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
assertFalse(dbf.isIgnoringElementContentWhitespace());
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
assertFalse(eh.isErrorOccured());
DOMSource domSource = new DOMSource(doc);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
SAXResult saxResult = new SAXResult();
try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
saxResult.setHandler(handler);
transformer.transform(domSource, saxResult);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例3: parse
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/** Parses the XML. It returns a Map.
* @param uri The location of the content to be parsed.
* @param validate if the XML is to be validated while parsing.
* @throws ParserConfigurationException if a DocumentBuilder cannot be created which satisfies the configuration requested.
* @throws SAXException If any parse errors occur.
* @throws IOException If any IO errors occur.
* @return The Map.
*/
public static Map parse(String uri, boolean validate)
throws ParserConfigurationException, SAXException, IOException {
// Create a factory object for creating DOM parsers
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Specifies that the parser produced by this factory will validate documents as they are parsed.
factory.setValidating(validate);
// Now use the factory to create a DOM parser
DocumentBuilder parser = factory.newDocumentBuilder();
// Specifies the EntityResolver to resolve DTD used in XML documents
parser.setEntityResolver(new DefaultEntityResolver());
// Specifies the ErrorHandler to handle warning/error/fatalError conditions
parser.setErrorHandler(new DefaultErrorHandler());
Document document = parser.parse(uri);
return parseXML(document);
}
示例4: testMoreUserInfo
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* Checking Text content in XML file.
* @see <a href="content/accountInfo.xml">accountInfo.xml</a>
*
* @throws Exception If any errors occur.
*/
@Test
public void testMoreUserInfo() throws Exception {
String xmlFile = XML_DIR + "accountInfo.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
dbf.setNamespaceAware(true);
dbf.setValidating(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler();
docBuilder.setErrorHandler(eh);
Document document = docBuilder.parse(xmlFile);
Element account = (Element)document
.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Account").item(0);
String textContent = account.getTextContent();
assertTrue(textContent.trim().regionMatches(0, "Rachel", 0, 6));
assertEquals(textContent, "RachelGreen744");
Attr accountID = account.getAttributeNodeNS(PORTAL_ACCOUNT_NS, "accountID");
assertTrue(accountID.getTextContent().trim().equals("1"));
assertFalse(eh.isAnyError());
}
示例5: createBuilder
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* Creates a new document builder.
*
* @return newly created document builder
*
* @throws XMLParserException thrown if their is a configuration error with the builder factory
*/
protected DocumentBuilder createBuilder() throws XMLParserException {
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
if (entityResolver != null) {
builder.setEntityResolver(entityResolver);
}
if (errorHandler != null) {
builder.setErrorHandler(errorHandler);
}
return builder;
} catch (ParserConfigurationException e) {
log.error("Unable to create new document builder", e);
throw new XMLParserException("Unable to create new document builder", e);
}
}
示例6: test1
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
@Test
public void test1() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);
dbf.setAttribute(SCHEMA_SOURCE, Bug4972882.class.getResource("targetNS00101m2_stub.xsd").toExternalForm());
DocumentBuilder builder = dbf.newDocumentBuilder();
builder.setErrorHandler(new DraconianErrorHandler());
try {
builder.parse(Bug4972882.class.getResource("targetNS00101m2_stub.xml").toExternalForm());
Assert.fail("failure expected");
} catch (SAXException e) {
Assert.assertTrue(e.getMessage().indexOf("sch-props-correct.2") != -1);
}
}
示例7: testGetTypeInfo
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* Check usage of TypeInfo interface introduced in DOM L3.
*
* @throws Exception If any errors occur.
*/
@Test
public void testGetTypeInfo() throws Exception {
String xmlFile = XML_DIR + "accountInfo.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
docBuilder.setErrorHandler(new MyErrorHandler());
Document document = docBuilder.parse(xmlFile);
Element userId = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "UserID").item(0);
TypeInfo typeInfo = userId.getSchemaTypeInfo();
assertTrue(typeInfo.getTypeName().equals("nonNegativeInteger"));
assertTrue(typeInfo.getTypeNamespace().equals(W3C_XML_SCHEMA_NS_URI));
Element role = (Element)document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Role").item(0);
TypeInfo roletypeInfo = role.getSchemaTypeInfo();
assertTrue(roletypeInfo.getTypeName().equals("BuyOrSell"));
assertTrue(roletypeInfo.getTypeNamespace().equals(PORTAL_ACCOUNT_NS));
}
示例8: convertToNodes
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
void convertToNodes() throws CanonicalizationException,
ParserConfigurationException, IOException, SAXException {
if (dfactory == null) {
dfactory = DocumentBuilderFactory.newInstance();
dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
dfactory.setValidating(false);
dfactory.setNamespaceAware(true);
}
DocumentBuilder db = dfactory.newDocumentBuilder();
// select all nodes, also the comments.
try {
db.setErrorHandler(new com.sun.org.apache.xml.internal.security.utils.IgnoreAllErrorHandler());
Document doc = db.parse(this.getOctetStream());
this.subNode = doc;
} catch (SAXException ex) {
// if a not-wellformed nodeset exists, put a container around it...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("<container>".getBytes("UTF-8"));
baos.write(this.getBytes());
baos.write("</container>".getBytes("UTF-8"));
byte result[] = baos.toByteArray();
Document document = db.parse(new ByteArrayInputStream(result));
this.subNode = document.getDocumentElement().getFirstChild().getFirstChild();
} finally {
if (this.inputOctetStreamProxy != null) {
this.inputOctetStreamProxy.close();
}
this.inputOctetStreamProxy = null;
this.bytes = null;
}
}
示例9: documentFrom
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* This method closes the given input stream upon completion.
*/
public static Document documentFrom(InputStream is)
throws SAXException, IOException, ParserConfigurationException {
is = new NamespaceRemovingInputStream(is);
// DocumentBuilderFactory is not thread safe
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// ensure that parser writes error/warning messages to the logger
// rather than stderr
builder.setErrorHandler(ERROR_HANDLER);
Document doc = builder.parse(is);
is.close();
return doc;
}
示例10: testCheckDocumentBuilderFactory16
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* Test the setValidating. By default it is false, use a
* document which is not valid. It should not throw a warning or an error.
* The test passes in case the return value equals 1.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory16() throws Exception {
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
assertFalse(eh.isErrorOccured());
}
示例11: readModuleDocument
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
private Document readModuleDocument(FileObject fo) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder parser = dbf.newDocumentBuilder();
parser.setEntityResolver(this);
parser.setErrorHandler(this);
InputStream is = fo.getInputStream();
Document document = parser.parse(is);
is.close();
return document;
}
示例12: testXmlParse
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
@Test
public void testXmlParse() {
ByteArrayInputStream is = new ByteArrayInputStream(
generatedXmlFile.getFormattedContent().getBytes());
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new TestEntityResolver());
builder.setErrorHandler(new TestErrorHandler());
builder.parse(is);
} catch (Exception e) {
fail("Generated XML File " + generatedXmlFile.getFileName() + " will not parse");
}
}
示例13: testCreateNewUser
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* Checking when creating an XML document using DOM Level 2 validating
* it without having a schema source or a schema location It must throw a
* sax parse exception.
*
* @throws Exception If any errors occur.
*/
@Test
public void testCreateNewUser() throws Exception {
String resultFile = USER_DIR + "accountInfoOut.xml";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler();
docBuilder.setErrorHandler(eh);
Document document = docBuilder.newDocument();
Element account = document.createElementNS(PORTAL_ACCOUNT_NS, "acc:Account");
Attr accountID = document.createAttributeNS(PORTAL_ACCOUNT_NS, "acc:accountID");
account.setAttributeNode(accountID);
account.appendChild(document.createElement("FirstName"));
account.appendChild(document.createElementNS(PORTAL_ACCOUNT_NS, "acc:LastName"));
account.appendChild(document.createElement("UserID"));
DOMImplementationLS impl
= (DOMImplementationLS) DOMImplementationRegistry
.newInstance().getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSParser builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
try(FileOutputStream output = new FileOutputStream(resultFile)) {
MyDOMOutput domOutput = new MyDOMOutput();
domOutput.setByteStream(output);
writer.write(account, domOutput);
docBuilder.parse(resultFile);
}
assertTrue(eh.isAnyError());
}
示例14: buildDocument
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* Validate the given stream and return a valid DOM document for parsing.
*/
protected Document buildDocument(ErrorHandler handler, InputStream stream)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
parser.setErrorHandler(handler);
return parser.parse(stream);
}
示例15: parse
import javax.xml.parsers.DocumentBuilder; //导入方法依赖的package包/类
/**
* parse data
*
* @param xmlPath
*
* @return
*
* @throws Exception
*/
public static InitDbConfig parse(InputStream xmlPath) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
Document document = null;
DocumentBuilder docBuilder = null;
docBuilder = factory.newDocumentBuilder();
DefaultHandler handler = new DefaultHandler();
docBuilder.setEntityResolver(handler);
docBuilder.setErrorHandler(handler);
document = docBuilder.parse(xmlPath);
List<String> schemaList = new ArrayList<>();
List<String> dataList = new ArrayList<>();
Element rootEl = document.getDocumentElement();
NodeList children = rootEl.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node node = children.item(i);
if (node instanceof Element) {
Element element = (Element) node;
if (elementNameMatch(element, "initialize-database")) {
schemaList = parseSchemaList(element);
} else if (elementNameMatch(element, "initialize-data")) {
dataList = parseDataList(element);
}
}
}
InitDbConfig initDbConfig = new InitDbConfig();
initDbConfig.setDataFileList(dataList);
initDbConfig.setSchemaFileList(schemaList);
return initDbConfig;
}