在Java中,使用XML解析器来处理XML文件是一种常见的方式。当删除XML节点后,可能会出现空白行仍然存在的情况。这是因为XML解析器在解析XML文件时会保留原始的格式,包括空白行和缩进。
要删除空白行,可以使用以下方法之一:
示例代码如下:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class RemoveWhitespace {
public static void main(String[] args) {
try {
// 加载XML文件
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("path/to/xml/file.xml"));
// 获取根节点
Element root = document.getDocumentElement();
// 遍历所有节点
removeWhitespaceNodes(root);
// 保存修改后的XML文件
saveDocument(document, "path/to/xml/file.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void removeWhitespaceNodes(Node node) {
NodeList children = node.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
Node child = children.item(i);
if (child.getNodeType() == Node.TEXT_NODE && child.getTextContent().trim().isEmpty()) {
node.removeChild(child);
} else if (child.getNodeType() == Node.ELEMENT_NODE) {
removeWhitespaceNodes(child);
}
}
}
private static void saveDocument(Document document, String filePath) throws IOException {
OutputStream outputStream = new FileOutputStream(filePath);
javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
javax.xml.transform.Result output = new javax.xml.transform.stream.StreamResult(outputStream);
javax.xml.transform.Source input = new javax.xml.transform.dom.DOMSource(document);
transformer.transform(input, output);
outputStream.close();
}
}
示例代码如下:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class RemoveWhitespace {
public static void main(String[] args) {
try {
// 加载XML文件
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(new File("path/to/xml/file.xml"), new RemoveWhitespaceHandler());
} catch (Exception e) {
e.printStackTrace();
}
}
private static class RemoveWhitespaceHandler extends DefaultHandler {
private StringBuilder content;
private OutputStream outputStream;
@Override
public void startDocument() throws SAXException {
content = new StringBuilder();
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
content.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (content.toString().trim().isEmpty()) {
try {
outputStream.write(("</" + qName + ">").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
outputStream.write(("<" + qName + ">").getBytes());
outputStream.write(content.toString().getBytes());
outputStream.write(("</" + qName + ">").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
content.setLength(0);
}
@Override
public void endDocument() throws SAXException {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void startDocument() throws SAXException {
try {
outputStream = new FileOutputStream("path/to/xml/file.xml");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上是两种常见的方法来删除XML节点后的空白行。根据具体的需求和场景选择适合的方法。
领取专属 10元无门槛券
手把手带您无忧上云