本文整理匯總了Java中org.w3c.dom.Document.normalize方法的典型用法代碼示例。如果您正苦於以下問題:Java Document.normalize方法的具體用法?Java Document.normalize怎麽用?Java Document.normalize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.w3c.dom.Document
的用法示例。
在下文中一共展示了Document.normalize方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: writeApplicationXml
import org.w3c.dom.Document; //導入方法依賴的package包/類
/**
* Writes the modified application back out to the file system.
*
* @param doc
* The application.xml DOM Document
* @throws org.apache.tools.ant.DeployException
* in case of any problems
*/
protected void writeApplicationXml(final Document doc) throws DeployException {
System.out.println("Writing out doc " + doc);
try {
doc.normalize();
// Prepare the DOM document for writing
DOMSource source = new DOMSource(doc);
// Prepare the output file
StreamResult result = new StreamResult(applicationXmlPath);
// Write the DOM document to the file
// Get Transformer
Transformer xformer = TransformerFactory.newInstance().newTransformer();
// Write to a file
xformer.transform(source, result);
} catch (TransformerException tex) {
throw new DeployException("Error writing out modified application xml", tex);
}
}
示例2: writeWebXml
import org.w3c.dom.Document; //導入方法依賴的package包/類
/**
* Writes the modified web.xml back out to war file
*
* @param doc
* The application.xml DOM Document
* @throws org.apache.tools.ant.DeployException
* in case of any problems
*/
protected void writeWebXml(final Document doc, final OutputStream outputStream) throws DeployException {
try {
doc.normalize();
// Prepare the DOM document for writing
DOMSource source = new DOMSource(doc);
// Prepare the output file
StreamResult result = new StreamResult(outputStream);
// Write the DOM document to the file
// Get Transformer
Transformer xformer = TransformerFactory.newInstance().newTransformer();
// Write to a file
xformer.transform(source, result);
} catch (TransformerException tex) {
throw new DeployException("Error writing out modified web xml ", tex);
}
}
示例3: readXml
import org.w3c.dom.Document; //導入方法依賴的package包/類
/**
* Reads (well-formed) list XML and checks it for basic validity:
* root node must be <code><rootTagName></code>
*/
private Document readXml(Reader reader) throws IOException, SAXException {
try {
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new InputSource(reader));
xml.normalize();
if (!xml.getDocumentElement().getNodeName().equals(rootTagName)) {
throw new IOException(Messages.XmlStringList_root_name_invalid);
}
return xml;
} catch (ParserConfigurationException ex) {
throw new IOException(ex);
}
}
示例4: writeXml
import org.w3c.dom.Document; //導入方法依賴的package包/類
private static void writeXml(final Document doc, final File file) throws IOException {
doc.normalize();
final TransformerFactory transformerFactory = TransformerFactory.newInstance(XML_FACTORY, null);
try {
final Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(file));
} catch (final TransformerException ex) {
throw new IOException(ex.getMessage(), ex);
}
}
示例5: load
import org.w3c.dom.Document; //導入方法依賴的package包/類
public static Document load(String filename) {
Document document = null;
try {
// 1.得到DOM解析器的工廠實例
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// 2.從DOM工廠裏獲取DOM解析器
DocumentBuilder builder = factory.newDocumentBuilder();
// 3.解析XML文檔,得到document,即DOM樹
document = builder.parse(new File(filename));
//Document對象調用normalize(),可以去掉XML文檔中作為格式化內容的空白而映射在DOM樹中的不必要的Text Node對象。
//否則你得到的DOM樹可能並不如你所想象的那樣。特別是在輸出的時候,這個normalize()更為有用。
document.normalize();
} catch (Exception ex) {
ex.printStackTrace();
}
return document;
}
示例6: removeWhitespace
import org.w3c.dom.Document; //導入方法依賴的package包/類
private void removeWhitespace(Document document) throws XPathExpressionException {
document.normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']",
document,
XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
Node node = nodeList.item(i);
node.getParentNode().removeChild(node);
}
}
示例7: compare
import org.w3c.dom.Document; //導入方法依賴的package包/類
/**
* Compares by DOM. Ordering requirements will be respected.
*/
boolean compare(Document document1, Document document2)
{
// Make sure adjacent text nodes are combined
document1.normalize();
document2.normalize();
return compareChildren(document1.getDocumentElement(), document2.getDocumentElement());
}
示例8: readDocument
import org.w3c.dom.Document; //導入方法依賴的package包/類
private static @NotNull Element readDocument(@NotNull File file) throws ParserConfigurationException, IOException, SAXException {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.parse(file);
document.normalize();
return document.getDocumentElement();
}