当前位置: 首页 > 工具软件 > GML4J > 使用案例 >

dom4j读取GML

赵珂
2023-12-01
<City xmlns="http://www.ukusa.org" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ukusa.org City.xsd">
		<Bridge>
			<span>100</span>
			<height>200</height>
			<gml:centerLineOf>
				<gml:LineString>
					<gml:pos>100 100</gml:pos>
					<gml:pos>200 300</gml:pos>
				</gml:LineString>
			</gml:centerLineOf>
			<mobility>DrawBridge1</mobility>
		</Bridge>
				<Bridge>
			<span>100</span>
			<height>200</height>
			<gml:centerLineOf>
				<gml:LineString>
					<gml:pos>100 100</gml:pos>
					<gml:pos>200 300</gml:pos>
				</gml:LineString>
			</gml:centerLineOf>
			<mobility>DrawBridge2</mobility>
		</Bridge>
				<Bridge>
			<span>100</span>
			<height>200</height>
			<gml:centerLineOf>
				<gml:LineString>
					<gml:pos>100 100</gml:pos>
					<gml:pos>200 300</gml:pos>
				</gml:LineString>
			</gml:centerLineOf>
			<mobility>DrawBridge3</mobility>
		</Bridge>
</City>
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.WKTReader;


public class Test3 {

	/**
	 * @param args
	 * @throws DocumentException 
	 */
	public static void main(String[] args) throws DocumentException {
		// TODO Auto-generated method stub
		Map map = new HashMap();
        map.put("","http://www.opengis.net/gml");
        SAXReader saxReader = new SAXReader();
        File file = new File("E:\\City.xml");
        
        Document document = saxReader.read(file);
        XPath x = document.createXPath("//gml:");
        x.setNamespaceURIs(map);
        
        Element root=document.getRootElement();
        for(Iterator ie = root.elementIterator(); ie.hasNext();)
		{/*Element skill = root.element("feature");*/
		 Element skill = (Element) ie.next();
      
		MyVisitor visitor = new MyVisitor();
		skill.accept(visitor);}
	}

}



解决GML前缀问题

输出几何部分:

<gml:LineString xmlns:gml="http://www.opengis.net/gml">
					<gml:pos>100 100</gml:pos>
					<gml:pos>200 300</gml:pos>
				</gml:LineString>
<gml:LineString xmlns:gml="http://www.opengis.net/gml">
					<gml:pos>100 100</gml:pos>
					<gml:pos>200 300</gml:pos>
				</gml:LineString>
<gml:LineString xmlns:gml="http://www.opengis.net/gml">
					<gml:pos>100 100</gml:pos>
					<gml:pos>200 300</gml:pos>
				</gml:LineString>




 类似资料: