本文整理匯總了Java中javax.xml.parsers.DocumentBuilderFactory.setIgnoringElementContentWhitespace方法的典型用法代碼示例。如果您正苦於以下問題:Java DocumentBuilderFactory.setIgnoringElementContentWhitespace方法的具體用法?Java DocumentBuilderFactory.setIgnoringElementContentWhitespace怎麽用?Java DocumentBuilderFactory.setIgnoringElementContentWhitespace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.parsers.DocumentBuilderFactory
的用法示例。
在下文中一共展示了DocumentBuilderFactory.setIgnoringElementContentWhitespace方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: get
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
public Document get(String url) throws Exception{
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
String user = api.getUser();String password = api.getPassword();
String authStr = user + ":" + password;
String authEncoded = Base64.encodeBase64String(authStr.getBytes());
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Basic " + authEncoded);
con.setDoOutput(true);
DocumentBuilderFactory factoryBuilder = DocumentBuilderFactory.newInstance();
factoryBuilder.setIgnoringComments(true);
factoryBuilder.setIgnoringElementContentWhitespace(true);
DocumentBuilder d = factoryBuilder.newDocumentBuilder();
Document document = d.parse(con.getInputStream());
return document;
}
示例2: compare
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
@Override
protected boolean compare(File baselineFile, File comparisonFile) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setCoalescing(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document baselineXml = builder.parse(baselineFile);
Document comparisonXml = builder.parse(comparisonFile);
baselineXml.normalizeDocument();
comparisonXml.normalizeDocument();
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreWhitespace(true);
return XMLUnit.compareXML(baselineXml, comparisonXml).similar();
} catch (SAXException | IOException | ParserConfigurationException e) {
throw new TransformationUtilityException("An exception happened when comparing the two XML files", e);
}
}
示例3: compareDocumentWithGold
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Compare contents of golden file with test output file by their document
* representation.
* Here we ignore the white space and comments. return true if they're
* lexical identical.
* @param goldfile Golden output file name.
* @param resultFile Test output file name.
* @return true if two file's document representation are identical.
* false if two file's document representation are not identical.
* @throws javax.xml.parsers.ParserConfigurationException if the
* implementation is not available or cannot be instantiated.
* @throws SAXException If any parse errors occur.
* @throws IOException if an I/O error occurs reading from the file or a
* malformed or unmappable byte sequence is read .
*/
public static boolean compareDocumentWithGold(String goldfile, String resultFile)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setCoalescing(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder db = factory.newDocumentBuilder();
Document goldD = db.parse(Paths.get(goldfile).toFile());
goldD.normalizeDocument();
Document resultD = db.parse(Paths.get(resultFile).toFile());
resultD.normalizeDocument();
return goldD.isEqualNode(resultD);
}
示例4: readXml
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/** Read XML as DOM.
*/
public static Document readXml(InputStream is)
throws SAXException, IOException, ParserConfigurationException
{
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(false);
dbf.setIgnoringElementContentWhitespace(true);
//dbf.setCoalescing(true);
//dbf.setExpandEntityReferences(true);
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
db.setEntityResolver( new NullResolver() );
// db.setErrorHandler( new MyErrorHandler());
Document doc = db.parse(is);
return doc;
}
示例5: testCheckElementContentWhitespace
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的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));
}
示例6: setDocument
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Loads the document used for signing/verification/redaction from the given input stream.
* <p>
* The document is checked against the given schema and the given error handler is used.
*
* @param inputStream the input stream
* @param schema the schema of the loaded XML document
* @param handler the errorhandler
* @throws RedactableXMLSignatureException if this RedactableXMLSignature object is not properly initialized
*/
public final void setDocument(InputStream inputStream, Schema schema, ErrorHandler handler)
throws RedactableXMLSignatureException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setSchema(schema);
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setIgnoringElementContentWhitespace(true);
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.setErrorHandler(handler);
setDocument(documentBuilder.parse(inputStream));
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new RedactableXMLSignatureException(e);
}
}
示例7: GlueSettingsParser
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
public GlueSettingsParser() throws ResourceParseException {
try {
setTypeMappings();
setStyleMappings();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
root = factory.newDocumentBuilder()
.parse(AjLatexMath.getAssetManager().open(RESOURCE_NAME))
.getDocumentElement();
parseGlueTypes();
} catch (Exception e) { // JDOMException or IOException
throw new XMLResourceParseException(RESOURCE_NAME, e);
}
}
示例8: root
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Reads a XML file and loads its content into a list of nodes
*
* @param fn Name of the XML file (relative to the executable's current path)
* @return Root of the XML tree corresponding to the loaded document, as a singleton list of nodes
*/
public static LinkedList<Node> root(String fn) {
LinkedList<Node> nodes = new LinkedList<>();
try {
// Remove quotes (first and last character)
File xmlFile = new File(fn.substring(1, fn.length() - 1));
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
// Ignore non-relevant whitespace (only works if the XML has an associated DTD)
docFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xmlFile);
// Normalize document
doc.getDocumentElement().normalize();
nodes.add(doc);
} catch (Exception e) {
return new LinkedList<>();
}
return nodes;
}
示例9: readXml
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Read XML as DOM.
*/
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(false);
dbf.setIgnoringElementContentWhitespace(true);
// dbf.setCoalescing(true);
// dbf.setExpandEntityReferences(true);
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
db.setEntityResolver(new NullResolver());
// db.setErrorHandler( new MyErrorHandler());
Document doc = db.parse(is);
return doc;
}
示例10: loadResourceAsXML
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Loads a XML
*
* @param name Name of the resource (without path)
* @return List of nodes corresponding to the children of the root of the loaded document
* @throws Exception Exception if input resource is not found or it has invalid format
*/
LinkedList<Node> loadResourceAsXML(String name) throws Exception {
FileInputStream xmlFile = getResource(name);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
// Ignore non-relevant whitespace
docFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xmlFile);
// Normalize document
doc.getDocumentElement().normalize();
// Add the children (XML resource should have a root which is ignored)
LinkedList<Node> nodes = new LinkedList<>();
NodeList nl = doc.getDocumentElement().getChildNodes();
for (int i = 0; i < nl.getLength(); ++i) {
nodes.add(nl.item(i));
}
return nodes;
}
示例11: initializePool
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Initializes the pool with a new set of configuration options.
*
* @throws XMLParserException thrown if there is a problem initialzing the pool
*/
protected synchronized void initializePool() throws XMLParserException {
if (!dirtyBuilderConfiguration) {
// in case the pool was initialized by some other thread
return;
}
DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
setAttributes(newFactory, builderAttributes);
setFeatures(newFactory, builderFeatures);
newFactory.setCoalescing(coalescing);
newFactory.setExpandEntityReferences(expandEntityReferences);
newFactory.setIgnoringComments(ignoreComments);
newFactory.setIgnoringElementContentWhitespace(ignoreElementContentWhitespace);
newFactory.setNamespaceAware(namespaceAware);
newFactory.setSchema(schema);
newFactory.setValidating(dtdValidating);
newFactory.setXIncludeAware(xincludeAware);
poolVersion++;
dirtyBuilderConfiguration = false;
builderFactory = newFactory;
builderPool.clear();
}
示例12: loadString
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
public static Document loadString(String paramString) throws Exception {
DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
localDocumentBuilderFactory.setIgnoringComments(false);
localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
localDocumentBuilderFactory.setValidating(false);
localDocumentBuilderFactory.setCoalescing(false);
DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
char[] arrayOfChar = new char[paramString.length()];
paramString.getChars(0, paramString.length(), arrayOfChar, 0);
InputSource localInputSource = new InputSource(new CharArrayReader(arrayOfChar));
return localDocumentBuilder.parse(localInputSource);
}
示例13: TeXSymbolParser
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
public TeXSymbolParser(InputStream file, String name)
throws ResourceParseException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
root = factory.newDocumentBuilder().parse(file)
.getDocumentElement();
// set possible valid symbol type mappings
setTypeMappings();
} catch (Exception e) { // JDOMException or IOException
throw new XMLResourceParseException(name, e);
}
}
示例14: getW3CXmlDoc
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Gets a org.w3c.dom.Document for this record. This method is optimized to create only one DOM when
* accessed multiple times for the same XMLDocReader.
*
* @return A org.w3c.dom.Document, or null if unable to read.
*/
public org.w3c.dom.Document getW3CXmlDoc() {
if (w3cXmlDoc != null)
return w3cXmlDoc;
DocumentBuilderFactory docfactory
= DocumentBuilderFactory.newInstance();
docfactory.setCoalescing(true);
docfactory.setExpandEntityReferences(true);
docfactory.setIgnoringComments(true);
docfactory.setNamespaceAware(true);
// We must set validation false since jdk1.4 parser
// doesn't know about schemas.
docfactory.setValidating(false);
// Ignore whitespace doesn't work unless setValidating(true),
// according to javadocs.
docfactory.setIgnoringElementContentWhitespace(false);
try {
DocumentBuilder docbuilder = docfactory.newDocumentBuilder();
w3cXmlDoc = docbuilder.parse(getXml());
} catch (Throwable e) {
return null;
}
return w3cXmlDoc;
}
示例15: convertToDocument
import javax.xml.parsers.DocumentBuilderFactory; //導入方法依賴的package包/類
/**
* Converts a given string into its document representation.
*
* @param string
* The String to be converted.
* @param nameSpaceAware
* Indicates whether namespaces have to be considered or not.
* @return The document representation of the string, <code>null</code> in
* case the input string was <code>null</code>.
* @throws ParserConfigurationException
* Thrown in case the conversion cannot be initiated.
* @throws IOException
* Thrown in case the reading of the string fails.
* @throws SAXException
* Thrown in case the string cannot be parsed.
*/
public static Document convertToDocument(String string,
boolean nameSpaceAware) throws ParserConfigurationException,
SAXException, IOException {
if (string == null) {
return null;
}
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(nameSpaceAware);
dfactory.setValidating(false);
dfactory.setIgnoringElementContentWhitespace(true);
dfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder builder = dfactory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(string)));
return doc;
}