<?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)
<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)
---------
输出:
2007-4-12
小红
小林
周末一起去吃火锅呀
------------
如果你的是JDK1.4,在解析XML可能会有一些问题,需要相应的修改源文件,具体的还没有深究。
谢谢大家帮忙 3Q~~~