本文整理匯總了Java中javax.xml.parsers.DocumentBuilderFactory.setNamespaceAware方法的典型用法代碼示例。如果您正苦於以下問題:Java DocumentBuilderFactory.setNamespaceAware方法的具體用法?Java DocumentBuilderFactory.setNamespaceAware怎麽用?Java DocumentBuilderFactory.setNamespaceAware使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.parsers.DocumentBuilderFactory
的用法示例。
在下文中一共展示了DocumentBuilderFactory.setNamespaceAware方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: load
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
private static Document load(InputStream in) throws IOException {
Document document = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(in);
} catch (ParserConfigurationException parserConfigurationException) {
parserConfigurationException.printStackTrace();
Assert.fail(parserConfigurationException.toString());
} catch (SAXException saxException) {
saxException.printStackTrace();
Assert.fail(saxException.toString());
}
return document;
}
示例2: toDom
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Convert the received jdom doc to a Document element.
*
* @param doc the doc
* @return the org.w3c.dom. document
*/
private org.w3c.dom.Document toDom(final Document doc) {
try {
final XMLOutputter xmlOutputter = new XMLOutputter();
final StringWriter elemStrWriter = new StringWriter();
xmlOutputter.output(doc, elemStrWriter);
final byte[] xmlBytes = elemStrWriter.toString().getBytes(Charset.defaultCharset());
final DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
dbf.setNamespaceAware(true);
return dbf.newDocumentBuilder().parse(
new ByteArrayInputStream(xmlBytes));
} catch (final Exception e) {
logger.trace(e.getMessage(), e);
return null;
}
}
示例3: testXpointerElementPos
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Test the XPointer element() framework with XInclude.
*
* @throws Exception If any errors occur.
*/
@Test(groups = {"readWriteLocalFiles"})
public void testXpointerElementPos() throws Exception {
String resultFile = USER_DIR + "doc_xpointer_element.out";
String goldFile = GOLDEN_DIR + "doc_xpointerGold.xml";
String xmlFile = XML_DIR + "doc_xpointer_element.xml";
try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
TransformerFactory.newInstance().newTransformer()
.transform(new DOMSource(db.parse(new File(xmlFile))),
new StreamResult(fos));
}
assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
示例4: engineVerifyFail
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
@Test
public void engineVerifyFail() throws Exception {
RedactableXMLSignature sig = RedactableXMLSignature.getInstance(algorithm);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new File("testdata/vehicles.sig.xml"));
NodeList nodeList = document.getElementsByTagNameNS(RedactableXMLSignature.XML_NAMESPACE, "Signature");
assertEquals(1, nodeList.getLength());
sig.initVerify(keyPair.getPublic());
sig.setDocument(document);
assertFalse(sig.verify());
validateXSD(document);
}
示例5: DomSerializer
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
public DomSerializer(DOMResult domResult) {
Node node = domResult.getNode();
if (node == null) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
domResult.setNode(doc);
serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
} catch (ParserConfigurationException pce) {
throw new TxwException(pce);
}
} else {
serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
}
}
示例6: validateApiResponseAgainstXsd
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Validates provided XML API response against the XSD {@link Schema}
*
* @param schema W3C XML {@link Schema} instance
* @param xmlApiResponsePayload XML API response payload
*/
public static void validateApiResponseAgainstXsd(Schema schema,
String xmlApiResponsePayload)
throws ParserConfigurationException, SAXException, IOException {
if(StringUtils.
isNotBlank(
xmlApiResponsePayload)) {
LOG.info("About to validate the API response against the XSD Schema ...");
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.
newInstance();
documentBuilderFactory.
setNamespaceAware(true);
schema.newValidator().
validate(
new DOMSource(
documentBuilderFactory.
newDocumentBuilder().
parse(new InputSource(
new StringReader(
xmlApiResponsePayload)))));
}
}
示例7: parseProp
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
private static Element parseProp(String value) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
ByteArrayInputStream is = new ByteArrayInputStream(value.getBytes(CharEncoding.UTF_8));
doc = builder.parse(is);
} catch (Exception e1) {
throw PersoniumCoreException.Dav.DAV_INCONSISTENCY_FOUND.reason(e1);
}
Element e = doc.getDocumentElement();
return e;
}
示例8: testcase05
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Unit test for XMLReader parsing when relative URI is used in xsl file and
* SystemId was set.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase05() throws Exception {
String outputFile = USER_DIR + "saxtf005.out";
String goldFile = GOLDEN_DIR + "saxtf005GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document document = docBuilder.parse(new File(XSLT_INCL_FILE));
Node node = (Node)document;
DOMSource domSource= new DOMSource(node);
domSource.setSystemId("file:///" + XML_DIR);
TransformerHandler handler =
saxTFactory.newTransformerHandler(domSource);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例9: setup
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
@BeforeClass
public void setup() throws Exception {
ifac = XMLInputFactory.newInstance();
ofac = XMLOutputFactory.newInstance();
tfac = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
db = dbf.newDocumentBuilder();
xml = Files.readAllBytes(Paths.get(XML_DIR + "cities.xml"));
template = Files.readAllBytes(Paths.get(XML_DIR + "cities.xsl"));
xmlDoc = db.parse(xmlInputStream());
}
示例10: setupDOMResultHandler
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Sets up handler for <code>DOMResult</code>.
*/
private void setupDOMResultHandler(DOMSource source, DOMResult result) throws SAXException {
// If there's no DOMResult, unset the validator handler
if (result == null) {
fDOMValidatorHandler = null;
fSchemaValidator.setDocumentHandler(null);
return;
}
final Node nodeResult = result.getNode();
// If the source node and result node are the same use the DOMResultAugmentor.
// Otherwise use the DOMResultBuilder.
if (source.getNode() == nodeResult) {
fDOMValidatorHandler = fDOMResultAugmentor;
fDOMResultAugmentor.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultAugmentor);
return;
}
if (result.getNode() == null) {
try {
DocumentBuilderFactory factory = fComponentManager.getFeature(Constants.ORACLE_FEATURE_SERVICE_MECHANISM) ?
DocumentBuilderFactory.newInstance() : new DocumentBuilderFactoryImpl();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
result.setNode(builder.newDocument());
}
catch (ParserConfigurationException e) {
throw new SAXException(e);
}
}
fDOMValidatorHandler = fDOMResultBuilder;
fDOMResultBuilder.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
示例11: 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;
}
示例12: data
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
@Parameters(name = "With test fixture: {0} and namespace: {1} and XML factory: {2}")
public static Collection<Object[]> data() {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderFactory nsAwareDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
nsAwareDocumentBuilderFactory.setNamespaceAware(true);
return asList(new Object[][] {
{ "simple", null, documentBuilderFactory},
{ "simple", new SimpleNamespaceContext(), documentBuilderFactory},
{ "ns-simple", new SimpleNamespaceContext(), nsAwareDocumentBuilderFactory},
{ "attr", null, documentBuilderFactory},
{ "attr", new SimpleNamespaceContext(), documentBuilderFactory},
});
}
示例13: testCheckingEncoding
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Check the UTF-16 XMLEncoding xml file.
*
* @throws Exception If any errors occur.
* @see <a href="content/movies.xml">movies.xml</a>
*/
@Test
public void testCheckingEncoding() throws Exception {
// Note since movies.xml is UTF-16 encoding. We're not using stanard XML
// file suffix.
String xmlFile = XML_DIR + "movies.xml.data";
try (InputStream source = bomStream("UTF-16", xmlFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document document = dbf.newDocumentBuilder().parse(source);
assertEquals(document.getXmlEncoding(), "UTF-16");
assertEquals(document.getXmlStandalone(), true);
}
}
示例14: createDom
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Creates a new DOM document.
*/
public static Document createDom() {
synchronized (DOMUtil.class) {
if (db == null) {
try {
DocumentBuilderFactory dbf = XmlUtil.newDocumentBuilderFactory();
dbf.setNamespaceAware(true);
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new FactoryConfigurationError(e);
}
}
return db.newDocument();
}
}
示例15: test1
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
@Test
public void test1() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
Document dom = parser.parse(Bug5073477.class.getResourceAsStream("Bug5073477.xml"));
DOMResult r = new DOMResult();
r.setNode(dom.getDocumentElement());
r.setNextSibling(r.getNode().getFirstChild());
}