前面有两篇博客都是讲的用前端js把XML解析出来,今天采用java的dom4j来在后台解析一个XMl,并且转成json格式后传到前台。
xml文件还是原来用的那一个(前两篇博文用到的),所以直接上java解析的代码:
package com.geoway.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.geoway.bean.PictureInfo;
import com.geoway.bean.PictureList;
import com.geoway.bean.PictureResources;
import com.geoway.bean.Tile;
import com.geoway.bean.WallMap;
import com.geoway.bean.WallMaps;
public class Parse {
//xml文件所在的路径
private static final String xmlPath = "WebRoot/xml/PictureResources.xml";
public static void main(String[] args){
parseToJSON(xmlPath);
}
//加载要解析的XML文件
public static Document LoadXML(String path){
SAXReader saxReader = new SAXReader();
Document doc = null;
try {
doc = saxReader.read(new File(path));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
public static void parseNodeWallMaps(Element root, PictureResources pictureResources){
//遍历解析WallMaps节点下的WallMap节点
List<WallMaps> wallMapsList = new ArrayList<WallMaps>();
List<Element> WallMapsEL = root.elements("WallMaps");
for(int i=0; i<WallMapsEL.size(); i++){
String node = WallMapsEL.get(i).attributeValue("node");
List<Element> maps = WallMapsEL.get(i).elements("WallMap");
List<WallMap> list = new ArrayList<WallMap>();
WallMaps wallMaps = new WallMaps();
wallMaps.setNode(node);
for(int j=0; j<maps.size(); j++){
Element item = (Element) maps.get(j);
String id = item.elementText("id");
String name = item.elementText("name");
String tileName = item.elementText("tileName");
String tileFolder = item.elementText("tileFolder");
String tileExtension = item.elementText("tileExtension");
String thumbImageName = item.elementText("thumbImageName");
String beginLevel = item.elementText("beginLevel");
String endLevel = item.elementText("endLevel");
String totalLevels = item.elementText("totalLevels");
WallMap wallMap = new WallMap();
wallMap.setId(id);
wallMap.setName(name);
wallMap.setTileName(tileName);
wallMap.setTileFolder(tileFolder);
wallMap.setTileExtension(tileExtension);
wallMap.setThumbImageName(thumbImageName);
wallMap.setBeginLevel(beginLevel);
wallMap.setEndLevel(endLevel);
wallMap.setTotalLevels(totalLevels);
list.add(wallMap);
}
wallMaps.setList(list);
wallMapsList.add(wallMaps);
}
pictureResources.setWallMaps(wallMapsList);
}
public static void parseNodePictureList(Element root, PictureResources pictureResources){
//遍历解析PictureList节点下的PictureInfo节点
List<PictureList> pictureList = new ArrayList<PictureList>();
List<Element> PictureListEL = root.elements("PictureList");
for(int i=0; i<PictureListEL.size(); i++){
String node = PictureListEL.get(i).attributeValue("node");
List<Element> pictureInfoList = PictureListEL.get(i).elements("PitureInfo");
PictureList pictureListBean = new PictureList();
pictureListBean.setNode(node);
PictureInfo pictureInfo = new PictureInfo();
for(int j=0; j<pictureInfoList.size(); j++){
Element item = (Element) pictureInfoList.get(j);
String id = item.elementText("id");
String name = item.elementText("name");
String dataName = item.elementText("dataName");
String thumbImageName = item.elementText("thumbImageName");
String coverName = item.elementText("coverName");
pictureInfo.setId(id);
pictureInfo.setName(name);
pictureInfo.setDataName(dataName);
pictureInfo.setThumbImageName(thumbImageName);
pictureInfo.setCoverName(coverName);
//遍历解析tiles
List<Tile> list = new ArrayList<Tile>();
List tilesList = pictureInfoList.get(j).element("Tiles").elements("Tile");
for(int k=0; k<tilesList.size(); k++){
Element tile = (Element) tilesList.get(k);
String tile_id = tile.elementText("id");
String tileName = tile.elementText("tileName");
String tileFolder = tile.elementText("tileFolder");
String tileExtension = tile.elementText("tileExtension");
String beginLevel = tile.elementText("beginLevel");
String endLevel = tile.elementText("endLevel");
String totalLevels = tile.elementText("totalLevels");
Tile myTile = new Tile();
myTile.setId(tile_id);
myTile.setTileName(tileName);
myTile.setTileFolder(tileFolder);
myTile.setTileExtension(tileExtension);
myTile.setBeginLevel(beginLevel);
myTile.setEndLevel(endLevel);
myTile.setTotalLevels(totalLevels);
list.add(myTile);
}
pictureInfo.setTiles(list);
}
pictureListBean.setPictureInfo(pictureInfo);
pictureList.add(pictureListBean);
}
pictureResources.setPictureLists(pictureList);
}
public static void printPictureResourcesToJSON(PictureResources pictureResources){
//以JSON格式输出
JSONObject jsonObject = JSONObject.fromObject(pictureResources);
String result = jsonObject.toString();
System.out.println("json: " + result);
}
//xml解析成json的函数
public static void parseToJSON(String path){
PictureResources pictureResources = new PictureResources();
//读取XML文件
Document doc = LoadXML(path);
//开始分析节点
Element root = doc.getRootElement();
Element BaseRootEL = root.element("BaseRoot");
String BaseRoot = BaseRootEL.getText();
pictureResources.setBaseRoot(BaseRoot);
Element WallMapRootEL = root.element("WallMapRoot");
String WallMapRoot = WallMapRootEL.getText();
pictureResources.setWallMapRoot(WallMapRoot);
Element PitureListRootEL = root.element("PictureListRoot");
String PitureListRoot = PitureListRootEL.getText();
pictureResources.setPictureListRoot(PitureListRoot);
parseNodeWallMaps(root, pictureResources);
parseNodePictureList(root, pictureResources);
printPictureResourcesToJSON(pictureResources);
}
}
大家会发现在上面的代码上引入了一些bean,这是为了把整个的json结构搭建起来创建的,结构很简单,在这里就不贴代码了,大家可以看着XML文件直接完成这些bean。