循环读取XML
来源:互联网 发布:淘宝懒人鞋阿迪 编辑:程序博客网 时间:2024/05/02 00:11
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.kerberos.KerberosKey;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.context.support.StaticApplicationContext;
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;
import org.xml.sax.SAXException;
public class Sample1 {
public static final String AGE = "age";
public static final String SEX = "sex";
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
String str = courseFile + "/beans.xml";
System.out.println(str);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(str);
Element root = doc.getDocumentElement();
NodeList users = root.getChildNodes();
Map<String, Object> allInfo = new HashMap<String, Object>();
allInfo = getXmlAll(allInfo, users);
System.out.println(allInfo);
}
public static Map<String, Object> getXmlAll(Map<String, Object> allInfo, NodeList nodeList) {
String stuNumb = "";
if (nodeList.getLength() > 0) {
Map<String, Object> stuMap = new HashMap<String, Object>();
for (int i = 0; i < nodeList.getLength(); i++) {
Map<String, Object> stuMapPro = new HashMap<String, Object>();
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attrs = node.getAttributes();
if (attrs != null && attrs.getLength() > 0) {
Node property = attrs.getNamedItem("id");
stuNumb = property.getNodeValue();
}
switch (node.getNodeName()) {
case AGE:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(AGE, node.getFirstChild().getNodeValue());
break;
case SEX:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(SEX, node.getFirstChild().getNodeValue());
break;
default:
NodeList childList = node.getChildNodes();
System.out.println(stuNumb);
if (childList.getLength() > 0) {
Map<String, Object> stuMap2 = new HashMap<String, Object>();
stuMap2.putAll(getXmlAll(stuMap, childList));
if (stuMap2.size() > 0) {
allInfo.put(stuNumb, stuMap2);
}
}
break;
}
}
if (stuMapPro.size() > 0) {
allInfo.putAll(stuMapPro);
}
}
}
return allInfo;
}
}
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.kerberos.KerberosKey;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.context.support.StaticApplicationContext;
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;
import org.xml.sax.SAXException;
public class Sample1 {
public static final String AGE = "age";
public static final String SEX = "sex";
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
String str = courseFile + "/beans.xml";
System.out.println(str);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(str);
Element root = doc.getDocumentElement();
NodeList users = root.getChildNodes();
Map<String, Object> allInfo = new HashMap<String, Object>();
allInfo = getXmlAll(allInfo, users);
System.out.println(allInfo);
}
public static Map<String, Object> getXmlAll(Map<String, Object> allInfo, NodeList nodeList) {
String stuNumb = "";
if (nodeList.getLength() > 0) {
Map<String, Object> stuMap = new HashMap<String, Object>();
for (int i = 0; i < nodeList.getLength(); i++) {
Map<String, Object> stuMapPro = new HashMap<String, Object>();
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
NamedNodeMap attrs = node.getAttributes();
if (attrs != null && attrs.getLength() > 0) {
Node property = attrs.getNamedItem("id");
stuNumb = property.getNodeValue();
}
switch (node.getNodeName()) {
case AGE:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(AGE, node.getFirstChild().getNodeValue());
break;
case SEX:
System.out.println(node.getFirstChild().getNodeValue());
stuMapPro.put(SEX, node.getFirstChild().getNodeValue());
break;
default:
NodeList childList = node.getChildNodes();
System.out.println(stuNumb);
if (childList.getLength() > 0) {
Map<String, Object> stuMap2 = new HashMap<String, Object>();
stuMap2.putAll(getXmlAll(stuMap, childList));
if (stuMap2.size() > 0) {
allInfo.put(stuNumb, stuMap2);
}
}
break;
}
}
if (stuMapPro.size() > 0) {
allInfo.putAll(stuMapPro);
}
}
}
return allInfo;
}
}
0 0
- 递归循环读取Xml
- 循环读取XML
- 循环读取XML文件
- asp.net 循环读取XML文档
- asp.net 循环读取XML文档
- .net 读取xml文件 循环遍历子节点
- 读取Xml文件,创建DataTable,递归循环Xml节点,添加数据到DataTable,绑定到GridView
- 读取Xml文件,创建DataTable,递归循环读取Xml数据添加到DataTable,绑定数据到GridView
- 读取XML
- 读取XML
- Xml 读取
- XML读取
- XML读取
- 读取XML
- 读取Xml
- XML读取
- 读取XML
- xml读取
- Java之RTTI机制的原理
- 1119. Pre- and Post-order Traversals
- 爬虫-漫画喵的100行逆袭
- group by 多个字段
- js 输出
- 循环读取XML
- leetcode_middle_56_240. Search a 2D Matrix II
- 1123. Is It a Complete AVL Tree
- 实现一个计数器,能够记录类创建了多少个对象
- 静态注册receiver时,不指定intent-filter
- Recursive sequence HDU
- 三星手机推迟新机发布市场将持续火爆
- PAT-B 1024. 科学计数法 (20)
- 图像卷积与滤波的一些知识点