我有一个xml字符串,我想从中删除空元素和包含该元素的行。
如此冷杉的例子:
XML:
<ct>
<c>http://192.168.105.213</c>
<l>http://192.168.105.213</l>
<o></o>
<l>http://192.168.105.213</l>
<o>http://192.168.105.213</o>
<ct>
在这个<o></o>
中是空元素,所以删除这个元素后我想要:
<ct>
<c>http://192.168.105.213</c>
<l>http://192.168.105.213</l>
<l>http://192.168.105.213</l>
<o>http://192.168.105.213</o>
<ct>
因此必须删除整行,以便缩进。
我试过了:xml.replaceAll("<(\\w+)></\\1>", ""));
这留下了一条空行:
<ct>
<c>http://192.168.105.213</c>
<l>http://192.168.105.213</l>
<l>http://192.168.105.213</l>
<o>http://192.168.105.213</o>
</ct>
如何正确删除空格或\n, \t, \r
以获得正确的缩进?
答案 0 :(得分:2)
这样可行:
xml.replaceAll("<(\\w+)></\\1>\n\\s+", ""));
它将匹配一个新行,后跟一个或多个空格(包括制表符),前面是您的模式。
编辑:xml.replaceAll("\n\\s+<(\\w+)></\\1>", "")
也应该适用于更深层次。
如果您希望根元素也是空的并且任何子元素都是无意的,您可能需要将换行符和空格设为可选
xml.replaceAll("\n?\\s*<(\\w+)></\\1>", "")
答案 1 :(得分:1)
这应该为你解决
xml.replaceAll("\n\t<(\\w+)></\\1>", "");
答案 2 :(得分:1)
正如评论中所建议的,重新考虑直接在HTML / XML文档上使用正则表达式,因为这些不是常规语言。相反,在解析的文本/值内容上使用正则表达式,但不转换文档。
一个很棒的XML操纵器工具是XSLT,转换语言和XPath的兄弟。 Java附带内置的XSLT 1.0处理器,也可以调用或提供外部处理器(Xalan, Saxon, etc.)。请考虑以下设置:
XSLT 脚本(另存为以下使用的.xsl文件;脚本删除空节点)
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- Identity Transform to Copy Document as is -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Empty Template to Remove Such Nodes -->
<xsl:template match="*[.='']"/>
</xsl:transform>
Java 代码
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.OutputKeys;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XMLTransform {
public static void main(String[] args) throws IOException, URISyntaxException,
SAXException, ParserConfigurationException,
TransformerException {
// Load XML and XSL Document
String inputXML = "path/to/Input.xml";
String xslFile = "path/to/XSLT/Script.xsl";
String outputXML = "path/to/Output.xml";
Source xslt = new StreamSource(new File(xslFile));
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File(inputXML));
// XSLT Transformation with pretty print
TransformerFactory prettyPrint = TransformerFactory.newInstance();
Transformer transformer = prettyPrint.newTransformer(xslt);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
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");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(outputXML));
transformer.transform(source, result);
}
}
<强>输出强>
<ct>
<c>http://192.168.105.213</c>
<l>http://192.168.105.213</l>
<l>http://192.168.105.213</l>
<o>http://192.168.105.213</o>
</ct>
<强> NAMESPACES 强>
使用名称空间时,例如以下XML:
<prefix:ct xmlns:prefix="http://www.example.com">
<c>http://192.168.105.213</c>
<l>http://192.168.105.213</l>
<o></o>
<l>http://192.168.105.213</l>
<o>http://192.168.105.213</o>
</prefix:ct>
在标题和添加的模板中使用以下带有声明的XSLT:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:prefix="http://www.example.com">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- Identity Transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Retain Namespace Prefix -->
<xsl:template match="ct">
<xsl:element name='prefix:{local-name()}' namespace='http://www.example.com'>
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<!-- Remove Empty Nodes -->
<xsl:template match="*[.='']"/>
</xsl:transform>
输出
<prefix:ct xmlns:prefix="http://www.example.com">
<c>http://192.168.105.213</c>
<l>http://192.168.105.213</l>
<l>http://192.168.105.213</l>
<o>http://192.168.105.213</o>
</prefix:ct>