SAX(Simple API for XML)
DOM(Document Object Model)
JDOM
DOM4J:
性能测试:(少量数据时)SAX>DOM>DOM4J>JDOM
解析xml格式的 sdl文件sld:ColorMapEntry 并且修改color属性颜色#F7FCB9更改成#000000,文件内容如下↓。
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
成功如下图所示,参数可以在接口改为动态的。
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();
}
}