用下面代码,并附中正确的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='&sup3;¤&Aring;&Ucirc;' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23002' name='&Igrave;&Ugrave;&frac14;×' >
<Role>0</Role> <Status>0</Status>
</Good> <Good id='23003' name='&Auml;§·¨' >
<Role>0</Role>
<Blood>0</Blood>
<Qigong>0</Qigong>
</Good>

<Good id='23004' name='&iquest;&oslash;&frac14;×' >
<Role>0</Role>
<Blood>0</Blood>
</Good> <Good id='23005' name='&Aring;&Ucirc;' >
</Good></Goods>

解决方案 »

  1.   

    不好意思,从editplus正复制出来变成乱码了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>