Dom创建与修改xml文件详解

一、基本概念

SAX(Simple API for XML)

  • 只能读,不能修改,只能顺序访问,适合解析大型XML,解析速度快
  • 常应用于处理大量数据的XML,实现异构系统的数据访问,实现跨平台
  • 从文档的开始通过每一节点移动,定位一个特定的节点

DOM(Document Object Model)

  • 不仅能读,还能修改,而且能够实现随机访问,缺点是解析速度慢,适合解析小型文档
  • 一般应用与小型的配置XML,方便操作
  • 为载入到内存的文档节点建立类型描述,呈现可横向移动、潜在巨大的树型结构
  • 在内存中生成节点树操作代价昂贵

JDOM

  •      仅使用具体类而不使用接口
  •      API大量使用了Collections类

DOM4J:

  •      是JDOM的一种智能分支,它合并了许多超出基本xml文档表示的功能
  •      使用接口和抽象基本类方法,是一个优秀的java xml API
  •      具有性能优异、灵活性好、功能强大和极端易用的特点
  •      是一个开放源代码的软件

二、关于性能

性能测试:(少量数据时)SAX>DOM>DOM4J>JDOM

三、功能详解

解析xml格式的 sdl文件sld:ColorMapEntry 并且修改color属性颜色#F7FCB9更改成#000000,文件内容如下↓。

Dom创建与修改xml文件详解_第1张图片

四、测试代码

package com.kero99.utils;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * dom解析sdl文件
 * @author ygc
 *
 */
public class DomTest {
		public static void main(String[] args) throws SAXException, IOException, TransformerException {
			//创建解析工厂
			DocumentBuilderFactory sdl=DocumentBuilderFactory.newInstance();		
			try {
				//创建文件解析
				DocumentBuilder file= sdl.newDocumentBuilder();
				//解析文件路径及名称
				Document  document= file.parse("D:\\cottonproduct.sld");
				NodeList list=document.getElementsByTagName("sld:ColorMapEntry");
				   for(int i=0;i

五、测试结果

成功如下图所示,参数可以在接口改为动态的。

Dom创建与修改xml文件详解_第2张图片

六、创建XML

package com.kero99.utils;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
 * 生成产量sld文件xml
 * @author ygc
 *
 */
public class CreateSld {
	public DocumentBuilder getDocumentBuilder(){
		// 创建一个DocumentBuilderFactory的对象
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		// 创建DocumentBuilder对象
		DocumentBuilder db =null;
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return db;
	}
	/**
	 * 生成xml
	 */
	public void CreateSldFile(){
		DocumentBuilder db = getDocumentBuilder();
		Document document = db.newDocument();
		document.setXmlStandalone(true);
		Element StyledLayerDescriptor = document.createElement("sld:StyledLayerDescriptor");
		StyledLayerDescriptor.setAttribute("xmlns","http://www.opengis.net/sld");
		StyledLayerDescriptor.setAttribute("xmlns:sld","http://www.opengis.net/sld");
		StyledLayerDescriptor.setAttribute("xmlns:gml", "http://www.opengis.net/gml");
		StyledLayerDescriptor.setAttribute("xmlns:ogc", "http://www.opengis.net/ogc");
		StyledLayerDescriptor.setAttribute("version", "1.0.0");
		//向bookstore根节点中添加子节点book
		Element UserLayer = document.createElement("sld:UserLayer");
		StyledLayerDescriptor.appendChild(UserLayer);
		Element LayerFeatureConstraints = document.createElement("sld:LayerFeatureConstraints");
		UserLayer.appendChild(LayerFeatureConstraints);
		
		Element FeatureTypeConstraint = document.createElement("sld:FeatureTypeConstraint");
		LayerFeatureConstraints.appendChild(FeatureTypeConstraint);

		Element UserStyle = document.createElement("sld:UserStyle");
		UserLayer.appendChild(UserStyle);
		Element Name = document.createElement("sld:Name");
		UserStyle.appendChild(Name);
		Name.setTextContent("yumi");
		Element FeatureTypeStyle = document.createElement("sld:FeatureTypeStyle");
		UserStyle.appendChild(FeatureTypeStyle);
		Element Name2 = document.createElement("sld:Name");
		Name2.setTextContent("name");
		FeatureTypeStyle.appendChild(Name2);
		Element FeatureTypeName = document.createElement("sld:FeatureTypeName");
		FeatureTypeName.setTextContent("Feature");
		FeatureTypeStyle.appendChild(FeatureTypeName);
		Element Rule = document.createElement("sld:Rule");
		FeatureTypeStyle.appendChild(Rule);
		Element RasterSymbolizer = document.createElement("sld:RasterSymbolizer");
		Rule.appendChild(RasterSymbolizer);
		document.appendChild(StyledLayerDescriptor);
		Element ColorMap = document.createElement("sld:ColorMap");
		RasterSymbolizer.appendChild(ColorMap);
		Element ColorMapEntry = document.createElement("sld:ColorMapEntry");
		ColorMapEntry.setAttribute("color","#FFFFE5");
		ColorMapEntry.setAttribute("opacity","0.0");
		Element ColorMapEntry1 = document.createElement("sld:ColorMapEntry");
		ColorMapEntry1.setAttribute("color","#F7FCB9");
		ColorMapEntry1.setAttribute("opacity","1.0");
		Element ColorMapEntry2 = document.createElement("sld:ColorMapEntry");
		ColorMapEntry2.setAttribute("color","#ADDD8E");
		ColorMapEntry2.setAttribute("opacity","1.0");
		Element ColorMapEntry3 = document.createElement("sld:ColorMapEntry");
		ColorMapEntry3.setAttribute("color","#87e670");
		ColorMapEntry3.setAttribute("opacity","1.0");
		Element ColorMapEntry4 = document.createElement("sld:ColorMapEntry");
		ColorMapEntry4.setAttribute("color","#00d65c");
		ColorMapEntry4.setAttribute("opacity","1.0");
		Element ColorMapEntry5 = document.createElement("sld:ColorMapEntry");
		ColorMapEntry5.setAttribute("color","#00ac6e");
		ColorMapEntry5.setAttribute("opacity","1.0");
		ColorMap.appendChild(ColorMapEntry);
		ColorMap.appendChild(ColorMapEntry1);
		ColorMap.appendChild(ColorMapEntry2);
		ColorMap.appendChild(ColorMapEntry3);
		ColorMap.appendChild(ColorMapEntry4);
		ColorMap.appendChild(ColorMapEntry5);
		
		//创建TransformerFactory对象
		TransformerFactory tff = TransformerFactory.newInstance();
		try {
			//创建Transformer对象
			Transformer tf = tff.newTransformer();
			tf.setOutputProperty(OutputKeys.INDENT, "yes");
			String uuid=UuidUtil.getUUID32();
			tf.transform(new DOMSource(document),new StreamResult(new File("D://"+uuid+".sld")));
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		CreateSld createSld = new CreateSld();
		createSld.CreateSldFile();
	}
}

七、创建结果

Dom创建与修改xml文件详解_第3张图片

你可能感兴趣的:(程序式界)