XML:
<?xml version="1.0" encoding="GB2312"?>
<test>
<user id="xiaoqiang" name="小强">
<re id="007">小而强大</re>
<re id="006">xx</re>
<re id="005">yy</re>
</user>
</test> JAVA:
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;class parsexml
{
private static parsexml par;
private parsexml()
{
}
public Map readXML(String XMLFile)throws Exception
{
System.out.println("*********** 读取XML文件 "+XMLFile+" ******************");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc=null;
try
{
doc = db.parse(XMLFile);
}
catch(Exception e)
{
System.out.println("无法读取文件:"+XMLFile+"!!");
System.exit(1);
}
Map testMap = new HashMap();
Element root = doc.getDocumentElement();
NodeList className = root.getElementsByTagName("user");
for(int i=0;i<className.getLength();i++)
{
TestBean test = new TestBean();
Element user = (Element)className.item(i);
Map map = new HashMap();
test.setId(user.getAttribute("id"));
test.setName(user.getAttribute("name"));
System.out.println("Id : "+user.getAttribute("id")+" Name : "+user.getAttribute("name"));
NodeList re = user.getElementsByTagName("re");
for(int j=0;j<re.getLength();j++)
{
ReBean rb = new ReBean();
Element r = (Element)re.item(j);
rb.setId(r.getAttribute("id"));
System.out.println("Re id : "+r.getAttribute("id"));
Text txt = (Text)r.getFirstChild();
rb.setValue(txt.getNodeValue());
System.out.println("Re is : "+txt.getNodeValue());
map.put(r.getAttribute("id"),rb);
}
test.setMap(map);
testMap.put(user.getAttribute("id"),test);
}
System.out.println("************* XML 文件解析完成 ***************");
return testMap;
}
public static parsexml getParseXML()
{
if(par==null)
{
return new parsexml();
}
return par;
}
public static void main(String[] args)
{
parsexml par =parsexml.getParseXML();
try
{
Map map = par.readXML("test.xml");
System.out.println("以下是从Map中取出的数据!!!");
TestBean tb = (TestBean)map.get("xiaoqiang");
System.out.println("这是TestBean中的数据 Id: "+tb.getId()+" Name: "+tb.getName());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}JavaBean:
写一个Bean把XML中的节点封装起来,用Map再封装Bean;
public class TestBean
{
private String Id;
private String Name;
private java.util.Map map;
public TestBean()
{
}
public TestBean(String Id,String Name,java.util.Map map)
{
this.setId(Id);
this.setName(Name);
this.setMap(map);
}
public void setId(String Id)
{
this.Id=Id;
}
public String getId()
{
return this.Id;
}
public void setName(String Name)
{
this.Name=Name;
}
public String getName()
{
return this.Name;
}
public void setMap(java.util.Map map)
{
this.map=map;
}
public java.util.Map getMap()
{
return this.map;
}
}
<?xml version="1.0" encoding="GB2312"?>
<test>
<user id="xiaoqiang" name="小强">
<re id="007">小而强大</re>
<re id="006">xx</re>
<re id="005">yy</re>
</user>
</test> JAVA:
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;class parsexml
{
private static parsexml par;
private parsexml()
{
}
public Map readXML(String XMLFile)throws Exception
{
System.out.println("*********** 读取XML文件 "+XMLFile+" ******************");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc=null;
try
{
doc = db.parse(XMLFile);
}
catch(Exception e)
{
System.out.println("无法读取文件:"+XMLFile+"!!");
System.exit(1);
}
Map testMap = new HashMap();
Element root = doc.getDocumentElement();
NodeList className = root.getElementsByTagName("user");
for(int i=0;i<className.getLength();i++)
{
TestBean test = new TestBean();
Element user = (Element)className.item(i);
Map map = new HashMap();
test.setId(user.getAttribute("id"));
test.setName(user.getAttribute("name"));
System.out.println("Id : "+user.getAttribute("id")+" Name : "+user.getAttribute("name"));
NodeList re = user.getElementsByTagName("re");
for(int j=0;j<re.getLength();j++)
{
ReBean rb = new ReBean();
Element r = (Element)re.item(j);
rb.setId(r.getAttribute("id"));
System.out.println("Re id : "+r.getAttribute("id"));
Text txt = (Text)r.getFirstChild();
rb.setValue(txt.getNodeValue());
System.out.println("Re is : "+txt.getNodeValue());
map.put(r.getAttribute("id"),rb);
}
test.setMap(map);
testMap.put(user.getAttribute("id"),test);
}
System.out.println("************* XML 文件解析完成 ***************");
return testMap;
}
public static parsexml getParseXML()
{
if(par==null)
{
return new parsexml();
}
return par;
}
public static void main(String[] args)
{
parsexml par =parsexml.getParseXML();
try
{
Map map = par.readXML("test.xml");
System.out.println("以下是从Map中取出的数据!!!");
TestBean tb = (TestBean)map.get("xiaoqiang");
System.out.println("这是TestBean中的数据 Id: "+tb.getId()+" Name: "+tb.getName());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}JavaBean:
写一个Bean把XML中的节点封装起来,用Map再封装Bean;
public class TestBean
{
private String Id;
private String Name;
private java.util.Map map;
public TestBean()
{
}
public TestBean(String Id,String Name,java.util.Map map)
{
this.setId(Id);
this.setName(Name);
this.setMap(map);
}
public void setId(String Id)
{
this.Id=Id;
}
public String getId()
{
return this.Id;
}
public void setName(String Name)
{
this.Name=Name;
}
public String getName()
{
return this.Name;
}
public void setMap(java.util.Map map)
{
this.map=map;
}
public java.util.Map getMap()
{
return this.map;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货