Dom解析XML文件数据

之前开发的时候有这个需求,应该是一年以前。没有写出来。然后最近又遇到这需求。完蛋,想不起来在哪个项目了。真心找死我了!归纳下:

首先,我需要解析的是一个xml的文件:

<CacheInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:typens="http://www.esri.com/schemas/ArcGIS/10.0" xsi:type="typens:CacheInfo">
<TileCacheInfo xsi:type="typens:TileCacheInfo">
<SpatialReference xsi:type="typens:GeographicCoordinateSystem">
<WKT>
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433],AUTHORITY["EPSG",4326]]
</WKT>
<XOrigin>-399.99999999999989</XOrigin>
<YOrigin>-399.99999999999989</YOrigin>
<XYScale>11258999068426.24</XYScale>
<ZOrigin>-100000</ZOrigin>
<ZScale>10000</ZScale>
<MOrigin>-100000</MOrigin>
<MScale>10000</MScale>
<XYTolerance>8.983152841195215e-009</XYTolerance>
<ZTolerance>0.001</ZTolerance>
<MTolerance>0.001</MTolerance>
<HighPrecision>true</HighPrecision>
<LeftLongitude>-180</LeftLongitude>
<WKID>4326</WKID>
</SpatialReference>
<TileOrigin xsi:type="typens:PointN">
<X>-400</X>
<Y>400</Y>
</TileOrigin>
<TileCols>256</TileCols>
<TileRows>256</TileRows>
<DPI>96</DPI>
<LODInfos xsi:type="typens:ArrayOfLODInfo">
<LODInfo xsi:type="typens:LODInfo">
<LevelID>0</LevelID>
<Scale>3500000</Scale>
<Resolution>0.0083281135204059786</Resolution>
</LODInfo>
<LODInfo xsi:type="typens:LODInfo">
<LevelID>1</LevelID>
<Scale>3000000</Scale>
<Resolution>0.0071383830174908385</Resolution>
</LODInfo>
</LODInfos>
</TileCacheInfo>
<TileImageInfo xsi:type="typens:TileImageInfo">
<CacheTileFormat>PNG8</CacheTileFormat>
<CompressionQuality>0</CompressionQuality>
<Antialiasing>true</Antialiasing>
</TileImageInfo>
<CacheStorageInfo xsi:type="typens:CacheStorageInfo">
<StorageFormat>esriMapCacheStorageModeExploded</StorageFormat>
<PacketSize>0</PacketSize>
</CacheStorageInfo>
</CacheInfo>

根据现有的xml文件,之后,我们现在需要解析里面的不同结点的数据出来。直接上现成的解析器。用的是Dom:

package cn.com.esrichina.xmlparse;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.PublicKey;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;
import android.util.Log;

/**
 * @author WXM
 * @data: 2014-6-11 下午3:12:09
 * @version: V1.0
 */
public class XmlParse {

	public static ArrayList<Double> scaless = new ArrayList<Double>(2);
	public static ArrayList<Double> resolutionn = new ArrayList<Double>(2);

	public XmlParse() {
		super();
		paeser();
		// Log.i("scaless:", scaless.toString());
		// Log.i("resolutionn", resolutionn.toString());
	}

	public void paeser() {
		// TODO Auto-generated method stub
		try {
			URL url = new URL(
					"http://127.0.0.1/cMap/cache/x/Layers/conf.xml");
			InputStream in = url.openStream();
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("GET");
			connection.setReadTimeout(5000);
			in = connection.getInputStream();

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;
			db = dbf.newDocumentBuilder();
			// InputStream stream = getResources().openRawResource(R.raw.conf);
			Document document = db.parse(in); // 存放该xml文件的路径;
			Element root = document.getDocumentElement();// 获得根元素
			System.out.println("根元素:" + root.getNodeName());
			// NodeList cacheInfo = document.getChildNodes();// 获得根元素下的子节点
			// Log.i("根元素下的子节点", cacheInfo.toString());

			MapConf mapConf = MapConf.getConf();

			NodeList WKID = document.getElementsByTagName("WKID");
			// Log.i("system.out", "WKID: " + WKID.item(0).getTextContent());
			mapConf.setWKID(WKID.item(0).getTextContent());

			NodeList X = document.getElementsByTagName("X");
			// Log.i("system.out", "TileOrigin X : " +
			// X.item(0).getTextContent());
			mapConf.setOriginX(Double.parseDouble(X.item(0).getTextContent()));

			NodeList Y = document.getElementsByTagName("Y");
			// Log.i("system.out", "TileOrigin Y : " +
			// Y.item(0).getTextContent());
			mapConf.setOriginY(Double.parseDouble(Y.item(0).getTextContent()));

			NodeList TileCols = document.getElementsByTagName("TileCols");
			// Log.i("system.out", "TileCols : "
			// + TileCols.item(0).getTextContent());
			mapConf.setTileCols(Integer.parseInt(TileCols.item(0)
					.getTextContent()));

			NodeList TileRows = document.getElementsByTagName("TileRows");
			// Log.i("system.out", "TileRows : "
			// + TileRows.item(0).getTextContent());
			mapConf.setTileRows(Integer.parseInt(TileRows.item(0)
					.getTextContent()));

			NodeList DPI = document.getElementsByTagName("DPI");
			// Log.i("system.out", "DPI : " + DPI.item(0).getTextContent());
			mapConf.setDPI(Integer.parseInt(DPI.item(0).getTextContent()));

			// 获取LOD的地图参数
			NodeList LODInfo = document.getElementsByTagName("LODInfo");
			for (int i = 0; i < LODInfo.getLength(); i++) {
				NodeList info = LODInfo.item(i).getChildNodes();
				for (int j = 0; j < info.getLength(); j++) {
					Node infoNode = info.item(j);
					// Log.i("system out mapconfig ",
					// infoNode.getTextContent());

					if ("LevelID".equals(infoNode.getNodeName())) {
						mapConf.setLevelID(Integer.parseInt(infoNode
								.getTextContent()));
						// lID[j] = mapConf.getLevelID();
						// Log.i("Value LevelID-->", " " +
						// mapConf.getLevelID());
						// Log.i("lID-->", " " + lID);
					} else if ("Scale".equals(infoNode.getNodeName())) {
						mapConf.setScale(Double.parseDouble(infoNode
								.getTextContent()));
						scaless.add(mapConf.getScale());

						// scales =mapConf.getScale();
						// Log.i("Value Scale-->", " " + mapConf.getScale());
					} else if ("Resolution".equals(infoNode.getNodeName())) {
						mapConf.setResolution(Double.parseDouble(infoNode
								.getTextContent()));
						resolutionn.add(mapConf.getResolution());

						// Log.i("Value Resolution-->",
						// " " + mapConf.getResolution());
					}
				}
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
以上就是一个XmlParse.java解析器了。其中有用到一个Model,一并附上源码:

package cn.com.esrichina.xmlparse;

public class MapConf {

	private String WKID;// 地图编码规范
	private double originX;// Tiles的原点X坐标
	private double originY;// Tiles的原点Y坐标
	private int tileCols;// 小切片的长的分辨率
	private int tileRows;// 小切片的宽的分辨率
	private int DPI;// 地图的DPI

	private int levelID;// 缩放等级
	private double scale;// 缩放的比例
	private double resolution;// 缩放的相对像素

	/*
	 * 
	 */
	private MapConf() {
	};

	private static MapConf mapConf = null;

	public synchronized static MapConf getConf() {
		if (mapConf == null) {
			mapConf = new MapConf();
		}
		return mapConf;
	}

	// -----------------------------

	public String getWKID() {
		return WKID;
	}

	public void setWKID(String WKID) {
		this.WKID = WKID;
	}

	public double getOriginX() {
		return originX;
	}

	public void setOriginX(double originX) {
		this.originX = originX;
	}

	public double getOriginY() {
		return originY;
	}

	public void setOriginY(double originY) {
		this.originY = originY;
	}

	public int getTileCols() {
		return tileCols;
	}

	public void setTileCols(int tileCols) {
		this.tileCols = tileCols;
	}

	public int getTileRows() {
		return tileRows;
	}

	public void setTileRows(int tileRows) {
		this.tileRows = tileRows;
	}

	public int getDPI() {
		return DPI;
	}

	public void setDPI(int DPI) {
		this.DPI = DPI;
	}

	// -------------
	public int getLevelID() {
		return levelID;
	}

	public int setLevelID(int levelID) {
		return this.levelID = levelID;
	}

	public double getScale() {
		return scale;
	}

	public void setScale(double scale) {
		this.scale = scale;
	}

	public double getResolution() {
		return resolution;
	}

	public void setResolution(double resolution) {
		this.resolution = resolution;
	}

}

以上实现的功能就是将xml的文件中的参数数据解析出来后存储在MapConf的Model中。在项目的其他地方进行数据的读取即可。

你可能感兴趣的:(xml,dom)