dom4j 创建一个带命名空间的pom.xml 文件
http://xml.apache.org/xalan-j/index.html
需要的jar包
<dependencies> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.7.2</version> </dependency> </dependencies>
示例
import java.io.IOException; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.xml.serializer.OutputPropertiesFactory; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentFactory; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Namespace; import org.dom4j.QName; import org.dom4j.io.DOMWriter; public class Dom4jDemo { public static void main(String[] args) throws IOException, TransformerFactoryConfigurationError, DocumentException, TransformerException { DocumentFactory documentFactory = DocumentFactory.getInstance(); Document document = documentFactory.createDocument(); Element projectElement = DocumentHelper.createElement("project"); Namespace noprefix = Namespace.get("http://maven.apache.org/POM/4.0.0"); Namespace xsi = Namespace.get("xsi", "http://www.w3.org/2001/XMLSchema-instance"); projectElement.add(noprefix); projectElement.add(xsi); projectElement.addAttribute(QName.get("schemaLocation", xsi), "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"); Element modelVersionElement = projectElement.addElement("modelVersion",noprefix.getURI()); modelVersionElement.addText("4.0.0"); Element groupIdElement = projectElement.addElement("groupId",noprefix.getURI()); groupIdElement.addText("cn.zno"); Element artifactIdElement = projectElement.addElement("artifactId",noprefix.getURI()); artifactIdElement.addText("z-test"); Element versionElement = projectElement.addElement("version", noprefix.getURI()); versionElement.addText("0.0.1-SNAPSHOT"); document.setRootElement(projectElement); StringWriter stringWriter = new StringWriter(); document.write(stringWriter); String onelineContent = stringWriter.toString().replaceAll(">\\s*<", "><"); String xalanVersion = org.apache.xalan.Version.getVersion(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4"); DOMSource domSource = new DOMSource(new DOMWriter().write(DocumentHelper.parseText(onelineContent))); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(domSource, streamResult); String transformedXmlContent = streamResult.getWriter().toString(); System.out.println(transformedXmlContent); } }
运行结果
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.zno</groupId> <artifactId>z-test</artifactId> <version>0.0.1-SNAPSHOT</version> </project>
PS:
1) org.dom4j.DocumentFactory 新建 document , javax.xml.parsers.DocumentBuilderFactory 也是新建 document
2)org.dom4j.DocumentHelper 是 易使用版 ,包含创建新的和创建已存在的
3)org.dom4j.io.SAXReader 可方便读取已有document
4) javax.xml.transform.Transformer 部分是java 的,正宗地址 http://xalan.apache.org/index.html