本文整理汇总了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();
}