本文整理汇总了Java中javax.xml.parsers.DocumentBuilderFactory.newInstance方法的典型用法代码示例。如果您正苦于以下问题:Java DocumentBuilderFactory.newInstance方法的具体用法?Java DocumentBuilderFactory.newInstance怎么用?Java DocumentBuilderFactory.newInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.parsers.DocumentBuilderFactory
的用法示例。
在下文中一共展示了DocumentBuilderFactory.newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: PaypalResponse
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Parses the response for Status and Preapproval Key.
*
* @param response
* The Response which is received in
* BaseServlet.sendPaypalRequest()
* @throws ParserConfigurationException
* SAX may throw this
* @throws SAXException
* SAX may throw this
* @throws IOException
* SAX may throw this
*/
public PaypalResponse(String response) throws ParserConfigurationException,
SAXException, IOException {
originalResponse = response;
final DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
final InputStream responseStream = new ByteArrayInputStream(
response.getBytes());
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document responseDocument = builder.parse(responseStream);
final Node documentElement = responseDocument.getDocumentElement();
final NodeList childs = documentElement.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
final Node child = childs.item(i);
if (checkNodeName(child, "preapprovalKey")) {
preapprovalKey = child.getTextContent().trim();
} else if (checkNodeName(child, "responseEnvelope")) {
final NodeList childs2 = child.getChildNodes();
for (int i2 = 0; i2 < childs2.getLength(); i2++) {
final Node child2 = childs2.item(i2);
if (checkNodeName(child2, "ack")) {
status = child2.getTextContent().trim();
} else if (checkNodeName(child2, "message")) {
error = child2.getTextContent().trim();
}
}
}
}
}
示例2: getMapFromXML
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
public static Map<String,Object> getMapFromXML(String xmlString) throws ParserConfigurationException, IOException, SAXException {
//这里用Dom的方式解析回包的最主要目的是防止API新增回包字段
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = IOUtils.toInputStream(xmlString);
Document document = builder.parse(is);
//获取到document里面的全部结点
NodeList allNodes = document.getFirstChild().getChildNodes();
Node node;
Map<String, Object> map = new HashMap<String, Object>();
int i=0;
while (i < allNodes.getLength()) {
node = allNodes.item(i);
if(node instanceof Element){
map.put(node.getNodeName(),node.getTextContent());
}
i++;
}
return map;
}
示例3: newDocumentBuilderFactory
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
public static DocumentBuilderFactory newDocumentBuilderFactory(boolean secureXmlProcessing) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isXMLSecurityDisabled(secureXmlProcessing));
} catch (ParserConfigurationException e) {
LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[] { factory.getClass().getName() } );
}
return factory;
}
示例4: 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, 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);
}
}
示例5: getVirtEnvName
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Private method to get the name of the virtual environment
* given in virtualenv tag of warhorn config file
*
* @param build Build
* @param workspace Jenkins job workspace
* @param listener Task listener
* @return virtEnvName Name of the Virtual Environment
* @throws ParserConfigurationException ParserConfigurationException
* @throws SAXException SAXException
* @throws IOException IOException
*/
private String getVirtEnvName(Run<?,?> build, FilePath workspace, TaskListener listener)
throws ParserConfigurationException, SAXException, IOException{
String absWarhornConfig = "";
if (configType.equals("configGit")){
absWarhornConfig = workspace.getRemote() + "/configRepo/" + gitConfigFile;
} else {
absWarhornConfig = workspace.getRemote() + "/warhorn_config.xml";
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(absWarhornConfig));
NodeList veNodeList = document.getElementsByTagName("virtualenv");
String virtEnvName = "";
if(veNodeList.getLength() > 0){
Element element = (Element) veNodeList.item(0);
virtEnvName = element.getAttribute("name");
}
return virtEnvName;
}
示例6: getDocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
if (dbfactory == null) {
dbfactory = DocumentBuilderFactory.newInstance();
}
if (dbld == null) {
dbld = dbfactory.newDocumentBuilder();
}
return dbld;
}
示例7: sign
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
public Document sign(FileInputStream fileStream, KeyPair keyPair)
throws ParserConfigurationException, SAXException, IOException,
NoSuchAlgorithmException, InvalidAlgorithmParameterException,
KeyException, MarshalException, XMLSignatureException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(fileStream);
DOMSignContext signContext = new DOMSignContext(keyPair.getPrivate(),
document.getDocumentElement());
XMLSignatureFactory signFactory = XMLSignatureFactory
.getInstance("DOM");
Reference ref = signFactory.newReference("", signFactory
.newDigestMethod(digestMethod, null), Collections
.singletonList(signFactory.newTransform(Transform.ENVELOPED,
(TransformParameterSpec) null)), null, null);
SignedInfo si = signFactory.newSignedInfo(signFactory
.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS,
(C14NMethodParameterSpec) null), signFactory
.newSignatureMethod(signatureMethod, null), Collections
.singletonList(ref));
KeyInfoFactory kif = signFactory.getKeyInfoFactory();
KeyValue kv = kif.newKeyValue(keyPair.getPublic());
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
XMLSignature signature = signFactory.newXMLSignature(si, ki);
signature.sign(signContext);
return document;
}
示例8: readXml
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
private static Document readXml(final File file) throws IOException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
return factory.newDocumentBuilder().parse(file);
} catch (ParserConfigurationException | SAXException ex) {
throw new IOException(ex.getMessage(), ex);
}
}
示例9: DomBuilder
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
private DomBuilder(String xml) throws ParserConfigurationException {
mXml = xml;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
DocumentBuilder docBuilder = factory.newDocumentBuilder();
mDocument = docBuilder.newDocument();
mDocument.setUserData(CONTENT_KEY, xml, null);
}
示例10: test
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
@Test
public void test() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document d = dbf.newDocumentBuilder().getDOMImplementation().createDocument(null, "r", null);
d.getDocumentElement().setAttributeNS("http://nowhere.net/", "id", "1");
Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(IDENTITY_XSLT)));
t.transform(new DOMSource(d), new StreamResult(new StringWriter()));
} catch (Throwable ex) {
Assert.fail("Exception: " + ex.getMessage());
}
}
示例11: documentFrom
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的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;
}
示例12: getDocument
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Returns the entire Document representing GUI data structure.
* @param model data structure
* @return complete GUI data structure in Document format
*/
public static Document getDocument(CommonModel model) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
return null;
}
Document modelDoc = docBuilder.newDocument();
// Writes all elements on Document
writeGuiInfos(modelDoc, model);
return modelDoc;
}
示例13: testCheckDocumentBuilderFactory07
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* Test the setExpandEntityReferences.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckDocumentBuilderFactory07() throws Exception {
try (FileInputStream fis = new FileInputStream(new File(
XML_DIR, "DocumentBuilderFactory02.xml"))) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(true);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
Document doc = docBuilder.parse(fis);
Element e = (Element) doc.getElementsByTagName("title").item(0);
NodeList nl = e.getChildNodes();
assertTrue(dbf.isExpandEntityReferences());
assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
}
}
示例14: testNewInstance02
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* To test exception thrown if javax.xml.parsers.DocumentBuilderFactory is
* invalid.
*/
@Test(expectedExceptions = FactoryConfigurationError.class)
public void testNewInstance02() {
DocumentBuilderFactory.newInstance();
}
示例15: newDocumentBuilderFactory
import javax.xml.parsers.DocumentBuilderFactory; //导入方法依赖的package包/类
/**
* 初始化一个DocumentBuilderFactory
*
* @return a DocumentBuilderFactory
*/
public static DocumentBuilderFactory newDocumentBuilderFactory() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
return dbf;
}