<?xml version="1.0" encoding="gb2312" ?> 
  <notes>
   <note date="2007-4-12">
  <from>小红</from> 
  <to>小林</to> 
  <message>周末一起去吃火锅呀</message> 
 </note>
 </notes>import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
class  DomXMLTest
{
public static void main(String[] args)
{   
try{
        DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
DocumentBuilder  builder=factory.newDocumentBuilder();
File f=new File("note.xml");
InputStream  is=new FileInputStream(f);
Document doc=builder.parse(is);
Element  root=doc.getDocumentElement();
NodeList  notes=root.getChildNodes();   for(int i=0;i<notes.getLength();i++)
  {
   Node note=notes.item(i);
    if(note.getNodeType()==Node.ELEMENT_NODE)
{
        String date =note.getAttributes().getNamedItem("date").getNodeValue();
            System.out.println(date);
        for(Node node=note.getFirstChild();node!=null;node=node.getNextSibling())
         {
                                if(node.getNodeType()==Node.ELEMENT_NODE)
 {
                                            if(node.getNodeName().equals("from"))
    {
 String from=node.getFirstChild().getNodeValue();
 System.out.println(from);
 }
if(node.getNodeName().equals("to"))
     {
  String to=node.getFirstChild().getNodeValue();
   System.out.println(to);
 }
 if(node.getNodeName().equals("message"))
     {
   String message=node.getFirstChild().getNodeValue();
   System.out.println(message);
  }
  }
   }
  } }
}
catch(ParserConfigurationException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
 }                                  
}
出现 错误  为什么  怎么  解决 
org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:264)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:98)
at DomXMLTest.main(DomXMLTest.java:27)

解决方案 »

  1.   

    <xml>文件加上</xml>关闭符另外把encoding改成"utf-8"再试试
      

  2.   

    楼上的不对,最大的可能是你的JDK版本过老,上面的程序我在JDK6+ECLIPSE3.2调试通过。
    ---------
    输出:
    2007-4-12
    小红
    小林
    周末一起去吃火锅呀
    ------------
    如果你的是JDK1.4,在解析XML可能会有一些问题,需要相应的修改源文件,具体的还没有深究。
      

  3.   

    我jdk是 1.5的   我也不知道原因啊,希望高手  解决一下  3Q
      

  4.   

    请问楼主是用ultraEdit保存这个xml的吗?出现这个问题是因为你的XML虽然保存为UTF-8,但是文件前面多了三个字节的bom,这是用来区分文件编码的,但是xml在被解析的时候不允许出现,解决的办法有多种,最简单的就是你重新用ultraedit选择另存为时,在格式那选择"utf-8 无bom"保存就可以了。这是我的经验,请试试。
      

  5.   

    知道啦    在<?xml version="1.0" encoding="gb2312" ?> 前面多个空格 所以不识别
    谢谢大家帮忙  3Q~~~