遞迴讀取xml 檔案;
阿新 • • 發佈:2018-12-16
package cn;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class CopyOfCopyOfTest {
private List<Page> pageList = new ArrayList<Page>();
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("d:\\catelog.xml"));
// 獲取根元素節點
Element root = doc.getDocumentElement();
// 這種讀取方式直接讀取整個節點; 相當於直接把所有的都讀取了; 不是像pullparser 一樣有個指標一樣的東西;
List<CatelogNode> childrennew = parseElement(root, null, null);
}
static List<CatelogNode> parseElement(Element element, CatelogNode parent,
List<CatelogNode> catelogNodeList) {
String tagName = element.getNodeName();
// System.out.print("<" + tagName);
List<Page> pageList = new ArrayList<Page>();
if ("Pages".equalsIgnoreCase(tagName)) {
// Page 物件集合;
NodeList children = element.getChildNodes();
Integer length = children.getLength();
for (int index = 0; index < length; index++) {
// 獲取每一個child Page 物件;
Node pageNode = children.item(index);
// 獲取節點型別
short nodeType = pageNode.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
// 這裡需要在這裡進行 new 否則 可能出現不匹配的情況;
Page page = new Page();
pageList.add(page);
NamedNodeMap map = pageNode.getAttributes();
// 如果存在屬性,則列印屬性
if (null != map) {
for (int i = 0; i < map.getLength(); i++) {
// 獲得該元素的每一個屬性
Attr attr = (Attr) map.item(i);
// 屬性名和屬性值
String attrName = attr.getName();
String attrValue = attr.getValue();
// 注意屬性值需要加上引號,所以需要\轉義
if ("name".equals(attrName)) {
page.setName(attrValue);
}
NodeList rootNodeList = pageNode.getChildNodes();
for (int rootIndex = 0; rootIndex < rootNodeList
.getLength(); rootIndex++) {
{
Node rootNode = rootNodeList
.item(rootIndex);
// 獲取節點型別
short rootNodeType = rootNode.getNodeType();
if (rootNodeType == Node.ELEMENT_NODE) {
NamedNodeMap rootMap = rootNode
.getAttributes();
Root root = new Root();
page.setRoot(root);
// 如果存在屬性,則列印屬性
if (null != rootMap) {
for (int rootAttrIndex = 0; rootAttrIndex < rootMap
.getLength(); rootAttrIndex++) {
Attr rootAttr = (Attr) rootMap
.item(rootAttrIndex);
// // 屬性名和屬性值
String rootAttrName = rootAttr
.getName();
String rootAttrValue = rootAttr
.getValue();
if ("name".equals(rootAttrName)) {
root.setName(rootAttrValue);
} else if ("tag"
.equals(rootAttrName)) {
root.setTag(rootAttrValue);
} else if ("bkImg"
.equals(rootAttrName)) {
root
.setBkImg(rootAttrValue);
}
}
}
// 便利直接的子節點;
NodeList menuNodeList = rootNode
.getChildNodes();
List<CatelogNode> directChildrenList = new ArrayList<CatelogNode>();
for (int menuIndex = 0; menuIndex < menuNodeList
.getLength(); menuIndex++) {
{
// ---------------------
try {
Node menuNode = menuNodeList
.item(menuIndex);
// 獲取節點型別
short menuNodeType = menuNode
.getNodeType();
if (menuNodeType == menuNode.ELEMENT_NODE) {
CatelogNode
node = toCatelogNode(menuNode);
System.out
.println("6----"
+ node
.getName()+"bk:"+node.getBkImg());
// 設定root 的 直接的catelogList
directChildrenList
.add(node);
List<CatelogNode> childrenList = new ArrayList<CatelogNode>();
//CatelogNode parentCatelogNode = toCatelogNode(menuNode);
// 遞迴所有的 直接的menu 形成list 返回;
List<CatelogNode> mynewList = toTree(
node,
childrenList,
menuNode);
node.setChildren(mynewList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
root
.setCatelogNodeList(directChildrenList);
for (int menuIndex = 0; menuIndex < menuNodeList
.getLength(); menuIndex++) {
{
// ---------------------
Node menuNode = rootNodeList
.item(rootIndex);
// 獲取節點型別
short menuNodeType = menuNode
.getNodeType();
try {
if (menuNodeType == menuNode.ELEMENT_NODE) {
// List<CatelogNode> childrenList = new ArrayList<CatelogNode>();
// CatelogNode parentCatelogNode = toCatelogNode(menuNode);
// // 遞迴所有的 直接的menu 形成list 返回;
// List<CatelogNode> mynewList = toTree(
// parentCatelogNode,
// childrenList,
// menuNode);
// for (int ik = 0; ik < mynewList
// .size(); ik++) {
// System.out
// .println(mynewList
// .get(ik)
//
// );
// }
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// System.out.println(root);
}
// attr end;
}
}
}
}
}
}
System.out.println("pageSize-------------" + pageList.size());
// for (int i = 0; i < pageList.size(); i++) {
//
// System.out
// .println("firstsize:---------------------"
// + i
// + "---------------"
// + pageList.get(i).getRoot()
// .getCatelogNodeList().size());
//
// System.out.println("se:" + i + "::::::::"
// + pageList.get(i).getRoot());
// }
List <CatelogNode> mylist=
pageList.get(0).getRoot().getCatelogNodeList();
for(int i=0;i<mylist.size();i++){
System.out.println(mylist.get(i));
}
}
return catelogNodeList;
}
private static CatelogNode toCatelogNode(Node menuNode) throws Exception {
short nodeType = menuNode.getNodeType();
CatelogNode currentNode = new CatelogNode();
if (nodeType == Node.ELEMENT_NODE) {
NamedNodeMap map = menuNode.getAttributes();
// 如果存在屬性,則列印屬性
if (null != map) {
for (int i = 0; i < map.getLength(); i++) {
// 獲得該元素的每一個屬性
Attr attr = (Attr) map.item(i);
// 屬性名和屬性值
String attrName = attr.getName();
String attrValue = attr.getValue();
// 注意屬性值需要加上引號,所以需要\轉義
// System.out.print(" " + attrName + "=\"" + attrValue +
// "\"");
if (currentNode != null) {// isMenu &&
if ("name".equals(attrName)) {
currentNode.setName(attrValue);
} else if ("tag".equals(attrName)) {
currentNode.setTag(attrValue);
} else if ("packFile".equals(attrName)) {
currentNode.setPackFile(attrValue);
} else if ("bkImg".equals(attrName)) {
currentNode.setBkImg(attrValue);
} else if ("btnPos".equals(attrName)) {
currentNode.setBtnPos(attrValue);
} else if ("btnSize".equals(attrName)) {
currentNode.setBtnSize(attrValue);
} else if ("rtnBtnPos".equals(attrName)) {
currentNode.setRtnBtnPos(attrValue);
} else if ("rtnBtnSize".equals(attrName)) {
currentNode.setRtnBtnSize(attrValue);
} else if ("homeBtnPos".equals(attrName)) {
currentNode.setHomeBtnPos(attrValue);
} else if ("homeBtnSize".equals(attrName)) {
currentNode.setHomeBtnSize(attrValue);
} else if ("type".equals(attrName)) {
currentNode.setType(attrValue);
} else if ("attr".equals(attrName)) {
currentNode.setAttr(attrValue);
}
}
}
}
}
return currentNode;
}
private static List<CatelogNode> toTree(CatelogNode parent,
List<CatelogNode> tempList, Node parentNode) throws Exception {
NodeList childrenList = parentNode.getChildNodes();
if (childrenList != null && childrenList.getLength() > 0) {
for (int i = 0; i < childrenList.getLength(); i++) {
Node node = childrenList.item(i);
// 獲取節點型別
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
CatelogNode chidl = toCatelogNode(node);
List<CatelogNode> sonList = new ArrayList<CatelogNode>();
chidl.setChildren(sonList);
tempList.add(chidl);
toTree(chidl, sonList, node);
}
}
}
return tempList;
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class CopyOfCopyOfTest {
private List<Page> pageList = new ArrayList<Page>();
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("d:\\catelog.xml"));
// 獲取根元素節點
Element root = doc.getDocumentElement();
// 這種讀取方式直接讀取整個節點; 相當於直接把所有的都讀取了; 不是像pullparser 一樣有個指標一樣的東西;
List<CatelogNode> childrennew = parseElement(root, null, null);
}
static List<CatelogNode> parseElement(Element element, CatelogNode parent,
List<CatelogNode> catelogNodeList) {
String tagName = element.getNodeName();
// System.out.print("<" + tagName);
List<Page> pageList = new ArrayList<Page>();
if ("Pages".equalsIgnoreCase(tagName)) {
// Page 物件集合;
NodeList children = element.getChildNodes();
Integer length = children.getLength();
for (int index = 0; index < length; index++) {
// 獲取每一個child Page 物件;
Node pageNode = children.item(index);
// 獲取節點型別
short nodeType = pageNode.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
// 這裡需要在這裡進行 new 否則 可能出現不匹配的情況;
Page page = new Page();
pageList.add(page);
NamedNodeMap map = pageNode.getAttributes();
// 如果存在屬性,則列印屬性
if (null != map) {
for (int i = 0; i < map.getLength(); i++) {
// 獲得該元素的每一個屬性
Attr attr = (Attr) map.item(i);
// 屬性名和屬性值
String attrName = attr.getName();
String attrValue = attr.getValue();
// 注意屬性值需要加上引號,所以需要\轉義
if ("name".equals(attrName)) {
page.setName(attrValue);
}
NodeList rootNodeList = pageNode.getChildNodes();
for (int rootIndex = 0; rootIndex < rootNodeList
.getLength(); rootIndex++) {
{
Node rootNode = rootNodeList
.item(rootIndex);
// 獲取節點型別
short rootNodeType = rootNode.getNodeType();
if (rootNodeType == Node.ELEMENT_NODE) {
NamedNodeMap rootMap = rootNode
.getAttributes();
Root root = new Root();
page.setRoot(root);
// 如果存在屬性,則列印屬性
if (null != rootMap) {
for (int rootAttrIndex = 0; rootAttrIndex < rootMap
.getLength(); rootAttrIndex++) {
Attr rootAttr = (Attr) rootMap
.item(rootAttrIndex);
// // 屬性名和屬性值
String rootAttrName = rootAttr
.getName();
String rootAttrValue = rootAttr
.getValue();
if ("name".equals(rootAttrName)) {
root.setName(rootAttrValue);
} else if ("tag"
.equals(rootAttrName)) {
root.setTag(rootAttrValue);
} else if ("bkImg"
.equals(rootAttrName)) {
root
.setBkImg(rootAttrValue);
}
}
}
// 便利直接的子節點;
NodeList menuNodeList = rootNode
.getChildNodes();
List<CatelogNode> directChildrenList = new ArrayList<CatelogNode>();
for (int menuIndex = 0; menuIndex < menuNodeList
.getLength(); menuIndex++) {
{
// ---------------------
try {
Node menuNode = menuNodeList
.item(menuIndex);
// 獲取節點型別
short menuNodeType = menuNode
.getNodeType();
if (menuNodeType == menuNode.ELEMENT_NODE) {
CatelogNode
node = toCatelogNode(menuNode);
System.out
.println("6----"
+ node
.getName()+"bk:"+node.getBkImg());
// 設定root 的 直接的catelogList
directChildrenList
.add(node);
List<CatelogNode> childrenList = new ArrayList<CatelogNode>();
//CatelogNode parentCatelogNode = toCatelogNode(menuNode);
// 遞迴所有的 直接的menu 形成list 返回;
List<CatelogNode> mynewList = toTree(
node,
childrenList,
menuNode);
node.setChildren(mynewList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
root
.setCatelogNodeList(directChildrenList);
for (int menuIndex = 0; menuIndex < menuNodeList
.getLength(); menuIndex++) {
{
// ---------------------
Node menuNode = rootNodeList
.item(rootIndex);
// 獲取節點型別
short menuNodeType = menuNode
.getNodeType();
try {
if (menuNodeType == menuNode.ELEMENT_NODE) {
// List<CatelogNode> childrenList = new ArrayList<CatelogNode>();
// CatelogNode parentCatelogNode = toCatelogNode(menuNode);
// // 遞迴所有的 直接的menu 形成list 返回;
// List<CatelogNode> mynewList = toTree(
// parentCatelogNode,
// childrenList,
// menuNode);
// for (int ik = 0; ik < mynewList
// .size(); ik++) {
// System.out
// .println(mynewList
// .get(ik)
//
// );
// }
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// System.out.println(root);
}
// attr end;
}
}
}
}
}
}
System.out.println("pageSize-------------" + pageList.size());
// for (int i = 0; i < pageList.size(); i++) {
//
// System.out
// .println("firstsize:---------------------"
// + i
// + "---------------"
// + pageList.get(i).getRoot()
// .getCatelogNodeList().size());
//
// System.out.println("se:" + i + "::::::::"
// + pageList.get(i).getRoot());
// }
List <CatelogNode> mylist=
pageList.get(0).getRoot().getCatelogNodeList();
for(int i=0;i<mylist.size();i++){
System.out.println(mylist.get(i));
}
}
return catelogNodeList;
}
private static CatelogNode toCatelogNode(Node menuNode) throws Exception {
short nodeType = menuNode.getNodeType();
CatelogNode currentNode = new CatelogNode();
if (nodeType == Node.ELEMENT_NODE) {
NamedNodeMap map = menuNode.getAttributes();
// 如果存在屬性,則列印屬性
if (null != map) {
for (int i = 0; i < map.getLength(); i++) {
// 獲得該元素的每一個屬性
Attr attr = (Attr) map.item(i);
// 屬性名和屬性值
String attrName = attr.getName();
String attrValue = attr.getValue();
// 注意屬性值需要加上引號,所以需要\轉義
// System.out.print(" " + attrName + "=\"" + attrValue +
// "\"");
if (currentNode != null) {// isMenu &&
if ("name".equals(attrName)) {
currentNode.setName(attrValue);
} else if ("tag".equals(attrName)) {
currentNode.setTag(attrValue);
} else if ("packFile".equals(attrName)) {
currentNode.setPackFile(attrValue);
} else if ("bkImg".equals(attrName)) {
currentNode.setBkImg(attrValue);
} else if ("btnPos".equals(attrName)) {
currentNode.setBtnPos(attrValue);
} else if ("btnSize".equals(attrName)) {
currentNode.setBtnSize(attrValue);
} else if ("rtnBtnPos".equals(attrName)) {
currentNode.setRtnBtnPos(attrValue);
} else if ("rtnBtnSize".equals(attrName)) {
currentNode.setRtnBtnSize(attrValue);
} else if ("homeBtnPos".equals(attrName)) {
currentNode.setHomeBtnPos(attrValue);
} else if ("homeBtnSize".equals(attrName)) {
currentNode.setHomeBtnSize(attrValue);
} else if ("type".equals(attrName)) {
currentNode.setType(attrValue);
} else if ("attr".equals(attrName)) {
currentNode.setAttr(attrValue);
}
}
}
}
}
return currentNode;
}
private static List<CatelogNode> toTree(CatelogNode parent,
List<CatelogNode> tempList, Node parentNode) throws Exception {
NodeList childrenList = parentNode.getChildNodes();
if (childrenList != null && childrenList.getLength() > 0) {
for (int i = 0; i < childrenList.getLength(); i++) {
Node node = childrenList.item(i);
// 獲取節點型別
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
CatelogNode chidl = toCatelogNode(node);
List<CatelogNode> sonList = new ArrayList<CatelogNode>();
chidl.setChildren(sonList);
tempList.add(chidl);
toTree(chidl, sonList, node);
}
}
}
return tempList;
}
}