用下面代码,并附中正确的xml文件/*
* Created on 2005-4-3
*/import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;/**
*
* @author yanbin.qiu
*/
public class TestJAXP
{ public static void main(String[] args)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String filename = "c:\\d.xml"; Document doc = db.parse(filename);
NodeList nl = doc.getElementsByTagName("Good"); for (int i = 0; i < nl.getLength(); i++)
{
NamedNodeMap nnm = nl.item(i).getAttributes();
if (nnm.getLength() > 1)
{
System.out.print("ID=>" + nnm.getNamedItem("id").getNodeValue());
System.out.println(" name=>" + nnm.getNamedItem("name").getNodeValue());
}
} }
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
d.xml<?xml version="1.0" encoding="GB2312"?>
<Goods>
<Belt/> <Good id='23001' name='³¤ÅÛ' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23002' name='ÌÙ¼×' >
<Role>0</Role> <Status>0</Status>
</Good> <Good id='23003' name='ħ·¨' >
<Role>0</Role>
<Blood>0</Blood>
<Qigong>0</Qigong>
</Good>
<Good id='23004' name='¿ø¼×' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23005' name='ÅÛ' >
</Good></Goods>
* Created on 2005-4-3
*/import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;/**
*
* @author yanbin.qiu
*/
public class TestJAXP
{ public static void main(String[] args)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String filename = "c:\\d.xml"; Document doc = db.parse(filename);
NodeList nl = doc.getElementsByTagName("Good"); for (int i = 0; i < nl.getLength(); i++)
{
NamedNodeMap nnm = nl.item(i).getAttributes();
if (nnm.getLength() > 1)
{
System.out.print("ID=>" + nnm.getNamedItem("id").getNodeValue());
System.out.println(" name=>" + nnm.getNamedItem("name").getNodeValue());
}
} }
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
d.xml<?xml version="1.0" encoding="GB2312"?>
<Goods>
<Belt/> <Good id='23001' name='³¤ÅÛ' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23002' name='ÌÙ¼×' >
<Role>0</Role> <Status>0</Status>
</Good> <Good id='23003' name='ħ·¨' >
<Role>0</Role>
<Blood>0</Blood>
<Qigong>0</Qigong>
</Good>
<Good id='23004' name='¿ø¼×' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23005' name='ÅÛ' >
</Good></Goods>
<Goods>
<Belt/> <Good id='23001' name='长袍' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23002' name='藤甲' >
<Role>0</Role> <Status>0</Status>
</Good> <Good id='23003' name='魔法' >
<Role>0</Role>
<Blood>0</Blood>
<Qigong>0</Qigong>
</Good>
<Good id='23004' name='盔甲' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23005' name='袍' >
</Good></Goods>