本文整理汇总了Java中javax.xml.parsers.DocumentBuilderFactory.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java DocumentBuilderFactory.setAttribute方法的具体用法?Java DocumentBuilderFactory.setAttribute怎么用?Java DocumentBuilderFactory.setAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.parsers.DocumentBuilderFactory
的用法示例。
在下文中一共展示了DocumentBuilderFactory.setAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testUserError
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Checking for Row 8 from the schema table when setting the schemaSource
* without the schemaLanguage must report an error.
*
* @throws Exception If any errors occur.
*/
@Test(expectedExceptions = IllegalArgumentException.class)
public void testUserError() throws Exception {
String xmlFile = XML_DIR + "userInfo.xml";
String schema = "http://java.sun.com/xml/jaxp/properties/schemaSource";
String schemaValue = "http://dummy.com/dummy.xsd";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute(schema, schemaValue);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
MyErrorHandler eh = new MyErrorHandler();
docBuilder.setErrorHandler(eh);
docBuilder.parse(xmlFile);
assertFalse(eh.isAnyError());
}
示例2: testGetTypeInfo
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的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));
}
示例3: testUsingDocumentBuilderFactory
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* XERCESJ-1141 root-type-definition property not read by XMLSchemaValidator during reset()
*/
@Test
public void testUsingDocumentBuilderFactory() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setAttribute(ROOT_TYPE, typeX);
dbf.setAttribute(DOCUMENT_CLASS_NAME,"com.sun.org.apache.xerces.internal.dom.PSVIDocumentImpl");
dbf.setNamespaceAware(true);
dbf.setValidating(false);
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
dbf.setSchema(sf.newSchema(fSchemaURL));
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fDocumentURL.toExternalForm());
ElementPSVI rootNode = (ElementPSVI) document.getDocumentElement();
assertValidity(ItemPSVI.VALIDITY_VALID, rootNode.getValidity());
assertValidationAttempted(ItemPSVI.VALIDATION_FULL, rootNode
.getValidationAttempted());
assertElementNull(rootNode.getElementDeclaration());
assertTypeName("X", rootNode.getTypeDefinition().getName());
}
示例4: setAttr
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
void setAttr(boolean setSrc) {
DocumentBuilderFactory docBFactory = DocumentBuilderFactory.newInstance();
Schema sch = createSchema();
docBFactory.setSchema(sch);
docBFactory.setNamespaceAware(true);
docBFactory.setValidating(true);
final String aSchemaLanguage = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
final String aSchemaSource = "http://java.sun.com/xml/jaxp/properties/schemaSource";
docBFactory.setAttribute(aSchemaLanguage, "http://www.w3.org/2001/XMLSchema");
// System.out.println("---- Set schemaLanguage: " +
// docBFactory.getAttribute(aSchemaLanguage));
if (setSrc) {
docBFactory.setAttribute(aSchemaSource, new InputSource(new StringReader(schemaSource)));
// System.out.println("---- Set schemaSource: " +
// docBFactory.getAttribute(aSchemaSource));
}
try {
docBFactory.newDocumentBuilder();
Assert.fail("ParserConfigurationException expected");
} catch (ParserConfigurationException pce) {
return; // success
}
}
示例5: test1
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的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, Bug4966143.class.getResource("Bug4966143.xsd").toExternalForm());
Document document = dbf.newDocumentBuilder().parse(Bug4966143.class.getResource("Bug4966143.xml").toExternalForm());
TypeInfo type = document.getDocumentElement().getSchemaTypeInfo();
Assert.assertFalse(type.isDerivedFrom("testNS", "Test", TypeInfo.DERIVATION_UNION));
Assert.assertFalse(type.isDerivedFrom("testNS", "Test", TypeInfo.DERIVATION_LIST));
Assert.assertFalse(type.isDerivedFrom("testNS", "Test", TypeInfo.DERIVATION_RESTRICTION));
Assert.assertTrue(type.isDerivedFrom("testNS", "Test", TypeInfo.DERIVATION_EXTENSION));
Assert.assertTrue(type.isDerivedFrom("testNS", "Test", 0));
}
示例6: testGetOwnerItemList
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Check grammar caching with imported schemas.
*
* @throws Exception If any errors occur.
* @see <a href="content/coins.xsd">coins.xsd</a>
* @see <a href="content/coinsImportMe.xsd">coinsImportMe.xsd</a>
*/
@Test
public void testGetOwnerItemList() throws Exception {
String xsdFile = XML_DIR + "coins.xsd";
String xmlFile = XML_DIR + "coins.xml";
try(FileInputStream fis = new FileInputStream(xmlFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
dbf.setValidating(false);
SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File(((xsdFile))));
MyErrorHandler eh = new MyErrorHandler();
Validator validator = schema.newValidator();
validator.setErrorHandler(eh);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(fis);
validator.validate(new DOMSource(document), new DOMResult());
assertFalse(eh.isAnyError());
}
}
示例7: testMoreUserInfo
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的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());
}
示例8: testCheckSchemaSupport2
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Test the default functionality of schema support method. In
* this case the schema source property is set.
* @throws Exception If any errors occur.
*/
@Test(dataProvider = "schema-source")
public void testCheckSchemaSupport2(Object schemaSource) throws Exception {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
W3C_XML_SCHEMA_NS_URI);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource);
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "test1.xml"));
assertFalse(eh.isErrorOccured());
} finally {
if (schemaSource instanceof Closeable) {
((Closeable) schemaSource).close();
}
}
}
示例9: createDocumentBuilderFactory
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
@Override
protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
throws ParserConfigurationException {
DocumentBuilderFactory factory = super.createDocumentBuilderFactory(validationMode, namespaceAware);
try {
factory.setAttribute(JAXP_SCHEMA_SOURCE, BLUEPRINT_SCHEMA);
} catch (IllegalArgumentException ex) {
log.warn("Cannot work with attribute " + JAXP_SCHEMA_SOURCE
+ " - configurations w/o a schema locations will likely fail to validate", ex);
}
return factory;
}
示例10: setAttributes
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Sets document builder attributes. If an attribute is not supported it is ignored.
*
* @param factory document builder factory upon which the attribute will be set
* @param attributes the set of attributes to be set
*/
protected void setAttributes(DocumentBuilderFactory factory, Map<String, Object> attributes) {
if (attributes == null || attributes.isEmpty()) {
return;
}
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
try {
log.debug("Setting DocumentBuilderFactory attribute '{}'", attribute.getKey());
factory.setAttribute(attribute.getKey(), attribute.getValue());
} catch (IllegalArgumentException e) {
log.warn("DocumentBuilderFactory attribute '{}' is not supported", attribute.getKey());
}
}
}
示例11: validate
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
public Boolean validate(File xmlDoc) {
this.xmlDoc = xmlDoc;
if (getSchema() == null || getXmlDoc() == null) {
return null;
}
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
try {
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE,getSchema());
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new ValidationErrorHandler());
valid = true;
builder.parse(getXmlDoc());
} catch (Exception ex) {
log.error(ex.toString());
logger.fatal("\n\nEl fichero de descripcion del XML "
+ xmlDoc
+ " no es valido. Compruebe la sintaxis de la descripcion y vuelva a intentarlo",ex);
valid = false;
}
return valid;
}
示例12: testGetOwnerInfo
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Check validation API features. A schema which is including in Bug 4909119
* used to be testing for the functionalities.
*
* @throws Exception If any errors occur.
* @see <a href="content/userDetails.xsd">userDetails.xsd</a>
*/
@Test
public void testGetOwnerInfo() throws Exception {
String schemaFile = XML_DIR + "userDetails.xsd";
String xmlFile = XML_DIR + "userDetails.xml";
try(FileInputStream fis = new FileInputStream(xmlFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(Paths.get(schemaFile).toFile());
Validator validator = schema.newValidator();
MyErrorHandler eh = new MyErrorHandler();
validator.setErrorHandler(eh);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
docBuilder.setErrorHandler(eh);
Document document = docBuilder.parse(fis);
DOMResult dResult = new DOMResult();
DOMSource domSource = new DOMSource(document);
validator.validate(domSource, dResult);
assertFalse(eh.isAnyError());
}
}
示例13: getDomBuilder
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Returns an instance of DocumentBuilder that may have set a Catalog.
*
* @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
* through the factory
* @param useCatalog the value of USE_CATALOG
* @param catalog a catalog
* @return an instance of DocumentBuilder
* @throws ParserConfigurationException
*/
DocumentBuilder getDomBuilder(boolean setUseCatalog, boolean useCatalog, String catalog)
throws ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
if (setUseCatalog) {
dbf.setFeature(XMLConstants.USE_CATALOG, useCatalog);
}
dbf.setAttribute(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
return docBuilder;
}
示例14: test1
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的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, Bug4966142.class.getResource("Bug4966142.xsd").toExternalForm());
Document document = dbf.newDocumentBuilder().parse(Bug4966142.class.getResource("Bug4966142.xml").toExternalForm());
TypeInfo type = document.getDocumentElement().getSchemaTypeInfo();
Assert.assertFalse(type.isDerivedFrom("testNS", "Test", TypeInfo.DERIVATION_UNION));
}
示例15: testCheckSchemaSupport1
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Test the default functionality of schema support method.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckSchemaSupport1() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
W3C_XML_SCHEMA_NS_URI);
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "test.xml"));
assertFalse(eh.isErrorOccured());
}