本文整理汇总了Java中javax.xml.parsers.DocumentBuilder类的典型用法代码示例。如果您正苦于以下问题:Java DocumentBuilder类的具体用法?Java DocumentBuilder怎么用?Java DocumentBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DocumentBuilder类属于javax.xml.parsers包,在下文中一共展示了DocumentBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: unsafeManualConfig3
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
public static void unsafeManualConfig3() throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://xml.org/sax/features/external-general-entities",true);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities",true);
//dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(getInputFile());
print(doc);
}
示例2: convertStringToDocument
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
/**
*
* Convert XML string to {@link Document}
*
* @param xmlString
* @return {@link Document}
*/
public static Document convertStringToDocument(String xmlString) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
factory.setFeature(Constants.DISALLOW_DOCTYPE_DECLARATION,true);
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new InputSource( new StringReader( xmlString ) ) );
return doc;
} catch (ParserConfigurationException| SAXException| IOException e) {
logger.debug("Unable to convert string to Document",e);
}
return null;
}
示例3: prepare
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
public void prepare() throws Exception {
this.funnyCreator.info("Extracting maven");
ZipUtils.unzipResource("/apache-maven-3.5.0.zip", FunnyConstants.MAVEN_DIRECTORY.getPath());
this.invoker = new DefaultInvoker();
this.invoker.setMavenHome(FunnyConstants.MAVEN_DIRECTORY);
if (!FunnyConstants.BUILD_DIRECTORY.exists()) {
FileUtils.forceMkdir(FunnyConstants.BUILD_DIRECTORY);
}
FunnyCreator.getLogger().info("Parse pom.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(FunnyConstants.REPOSITORY_DIRECTORY, "pom.xml"));
doc.getDocumentElement().normalize();
this.projectElement = (Element) doc.getElementsByTagName("project").item(0);
}
示例4: XMLPackedSheet
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
/**
* Create a new XML packed sheet from the XML output by the slick tool
*
* @param imageRef The reference to the image
* @param xmlRef The reference to the XML
* @throws SlickException Indicates a failure to parse the XML or read the image
*/
public XMLPackedSheet(String imageRef, String xmlRef) throws SlickException
{
image = new Image(imageRef, false, Image.FILTER_NEAREST);
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(ResourceLoader.getResourceAsStream(xmlRef));
NodeList list = doc.getElementsByTagName("sprite");
for (int i=0;i<list.getLength();i++) {
Element element = (Element) list.item(i);
String name = element.getAttribute("name");
int x = Integer.parseInt(element.getAttribute("x"));
int y = Integer.parseInt(element.getAttribute("y"));
int width = Integer.parseInt(element.getAttribute("width"));
int height = Integer.parseInt(element.getAttribute("height"));
sprites.put(name, image.getSubImage(x,y,width,height));
}
} catch (Exception e) {
throw new SlickException("Failed to parse sprite sheet XML", e);
}
}
示例5: test
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
@Test(dataProvider = "illegalCharactersData")
public void test(int character) throws Exception {
// Construct the XML document as a String
int[] cps = new int[]{character};
String txt = new String(cps, 0, cps.length);
String inxml = "<topElement attTest=\'" + txt + "\'/>";
String exceptionText = "NO EXCEPTION OBSERVED";
String hexString = "0x" + Integer.toHexString(character);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource isrc = new InputSource(new StringReader(inxml));
try {
db.parse(isrc);
} catch (SAXException e) {
exceptionText = e.toString();
}
System.out.println("Got Exception:" + exceptionText);
assertTrue(exceptionText.contains("attribute \"attTest\""));
assertTrue(exceptionText.contains("element is \"topElement\""));
assertTrue(exceptionText.contains("Unicode: " + hexString));
}
示例6: wrapWithSoapEnvelope
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
public Document wrapWithSoapEnvelope(Element element) {
DocumentBuilder documentBuilder;
try {
documentBuilder = newDocumentBuilder();
} catch (ParserConfigurationException e) {
LOG.error("*** ALERT: Failed to create a document builder when trying to construct the the soap message. ***", e);
throw propagate(e);
}
Document document = documentBuilder.newDocument();
document.adoptNode(element);
Element envelope = document.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Envelope");
Element body = document.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "soapenv:Body");
envelope.appendChild(body);
body.appendChild(element);
document.appendChild(envelope);
return document;
}
示例7: testWriteXml
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
@Test
public void testWriteXml() throws Exception {
StringWriter sw = new StringWriter();
ConfServlet.writeResponse(getTestConf(), sw, "xml");
String xml = sw.toString();
DocumentBuilderFactory docBuilderFactory
= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));
NodeList nameNodes = doc.getElementsByTagName("name");
boolean foundSetting = false;
for (int i = 0; i < nameNodes.getLength(); i++) {
Node nameNode = nameNodes.item(i);
String key = nameNode.getTextContent();
System.err.println("xml key: " + key);
if (TEST_KEY.equals(key)) {
foundSetting = true;
Element propertyElem = (Element)nameNode.getParentNode();
String val = propertyElem.getElementsByTagName("value").item(0).getTextContent();
assertEquals(TEST_VAL, val);
}
}
assertTrue(foundSetting);
}
示例8: testGetInputEncoding002
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
/**
* Assertion testing
* for public String getInputEncoding(),
* Encoding is not specified. getInputEncoding returns null..
*/
@Test
public void testGetInputEncoding002() {
Document doc = null;
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = db.newDocument();
} catch (ParserConfigurationException e) {
Assert.fail(e.toString());
}
String encoding = doc.getInputEncoding();
if (encoding != null) {
Assert.fail("expected encoding: null, returned: " + encoding);
}
System.out.println("OK");
}
示例9: FictionBook
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
public FictionBook(File file) throws ParserConfigurationException, IOException, SAXException, OutOfMemoryError {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream inputStream = new FileInputStream(file);
BufferedReader br = new BufferedReader(new FileReader(file));
String encoding = "utf-8";
try {
String line = br.readLine();
encoding = line.substring(line.indexOf("encoding=\"") + 10, line.indexOf("\"?>"));
} catch (Exception e) {
e.printStackTrace();
}
Document doc = db.parse(new InputSource(new InputStreamReader(inputStream, encoding)));
initXmlns(doc);
description = new Description(doc);
NodeList bodyNodes = doc.getElementsByTagName("body");
for (int item = 0; item < bodyNodes.getLength(); item++) {
bodies.add(new Body(bodyNodes.item(item)));
}
NodeList binary = doc.getElementsByTagName("binary");
for (int item = 0; item < binary.getLength(); item++) {
Binary binary1 = new Binary(binary.item(item));
binaries.put(binary1.getId().replace("#", ""), binary1);
}
}
示例10: parseUtfXmlFile
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
/**
* Parses the given UTF file as a DOM document, using the JDK parser. The parser does not
* validate, and is optionally namespace aware.
*
* @param file the UTF encoded file to parse
* @param namespaceAware whether the parser is namespace aware
* @return the DOM document
*/
@NonNull
public static Document parseUtfXmlFile(@NonNull File file, boolean namespaceAware)
throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Reader reader = getUtfReader(file);
try {
InputSource is = new InputSource(reader);
factory.setNamespaceAware(namespaceAware);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(is);
} finally {
reader.close();
}
}
示例11: parseProp
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
private Element parseProp(String value) {
// valをDOMでElement化
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;
}
示例12: main
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
DOMImplementation impl = document.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer dsi = implLS.createLSSerializer();
/* We should have here incorrect document without getXmlVersion() method:
* Such Document is generated by replacing the JDK bootclasses with it's
* own Node,Document and DocumentImpl classes (see run.sh). According to
* XERCESJ-1007 the AbstractMethodError should be thrown in such case.
*/
String result = dsi.writeToString(document);
System.out.println("Result:" + result);
}
示例13: GuiData
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
protected GuiData() {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(FEFEditor.class.getResourceAsStream("data/xml/FileTypes.xml"));
doc.getDocumentElement().normalize();
NodeList nList = doc.getDocumentElement().getElementsByTagName("FatesTypes").item(0).getChildNodes();
for (int x = 0; x < nList.getLength(); x++) {
if (nList.item(x).getNodeType() == Node.ELEMENT_NODE)
baseTypes.add(nList.item(x).getAttributes().getNamedItem("name").getNodeValue());
}
nList = doc.getDocumentElement().getElementsByTagName("DLC").item(0).getChildNodes();
for (int x = 0; x < nList.getLength(); x++) {
if (nList.item(x).getNodeType() == Node.ELEMENT_NODE)
dlcTypes.add(nList.item(x).getAttributes().getNamedItem("name").getNodeValue());
}
nList = doc.getDocumentElement().getElementsByTagName("Awakening").item(0).getChildNodes();
for (int x = 0; x < nList.getLength(); x++) {
if (nList.item(x).getNodeType() == Node.ELEMENT_NODE)
awakeningTypes.add(nList.item(x).getAttributes().getNamedItem("name").getNodeValue());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例14: compareFile
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
public void compareFile(String fileLeft, String fileRight) throws SAXException, IOException, ParserConfigurationException, TransformerException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setCoalescing(true);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setIgnoringComments(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc1 = db.parse(new File(expectedPath + "/" + fileRight + ".xml"));
doc1.normalizeDocument();
Document doc2 = db.parse(new File(OUT_DIR + "/" + fileLeft + ".xml"));
doc2.normalizeDocument();
assertEquals("Output <" + fileLeft + "> is not the same as expected output <" +
fileRight + ">",
docToString(doc1),
docToString(doc2));
}
示例15: testJobTaskCountersXML
import javax.xml.parsers.DocumentBuilder; //导入依赖的package包/类
@Test
public void testJobTaskCountersXML() throws Exception {
WebResource r = resource();
Map<JobId, Job> jobsMap = appContext.getAllJobs();
for (JobId id : jobsMap.keySet()) {
String jobId = MRApps.toString(id);
for (Task task : jobsMap.get(id).getTasks().values()) {
String tid = MRApps.toString(task.getID());
ClientResponse response = r.path("ws").path("v1").path("mapreduce")
.path("jobs").path(jobId).path("tasks").path(tid).path("counters")
.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
String xml = response.getEntity(String.class);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document dom = db.parse(is);
NodeList info = dom.getElementsByTagName("jobTaskCounters");
verifyAMTaskCountersXML(info, task);
}
}
}