我用加入后("org.apache.xerces.parsers.SAXParser")解析通过你的代码我修改后
package selfxmlparser;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import org.xml.*;
import org.xml.sax.*;
import java.io.*;
import org.xml.sax.helpers.*;
import java.util.Stack;
import javax.xml.parsers.*;
import org.jdom.output.XMLOutputter;
import java.util.Date;import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.xml.sax.ContentHandler;
import java.io.*;
import javax.xml.parsers.*;public class Example2 extends DefaultHandler
{ public String name = "";
public String location = ""; private CharArrayWriter contents = new CharArrayWriter(); public void startElement(
String namespaceURI,
String localName,
String qName,
Attributes attr)
throws SAXException
{ contents.reset(); } public void endElement(String namespaceURI, String localName, String qName)
throws SAXException
{ if (localName.equals("name"))
{
name = contents.toString();
} if (localName.equals("location"))
{
location = contents.toString();
} } public void characters(char[] ch, int start, int length) throws SAXException
{
contents.write(ch, start, length);
} public static void main(String[] argv)
{
System.out.println("Example2:");
try
{
System.out.println("No.1");
XMLReader xr = XMLReaderFactory.createXMLReader
// ("javax.xml.parsers.SAXParser");
("org.apache.xerces.parsers.SAXParser");
System.out.println("No.2");
Example2 ex2 = new Example2();
System.out.println("No.3");
xr.setContentHandler(ex2);
System.out.println("No.4");
xr.parse(new InputSource(new FileReader("c:\\Example2.xml")));
System.out.println("No.5");
System.out.println("Hello World from " + ex2.name + " in " + ex2.location);
}
catch (Exception e)
{
System.out.println("123");
e.printStackTrace();
System.out.println("456");
}
}}
-----------------------------
Example2:No.1No.2No.3No.4No.5Hello World from Bob in New York
package selfxmlparser;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import org.xml.*;
import org.xml.sax.*;
import java.io.*;
import org.xml.sax.helpers.*;
import java.util.Stack;
import javax.xml.parsers.*;
import org.jdom.output.XMLOutputter;
import java.util.Date;import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.xml.sax.ContentHandler;
import java.io.*;
import javax.xml.parsers.*;public class Example2 extends DefaultHandler
{ public String name = "";
public String location = ""; private CharArrayWriter contents = new CharArrayWriter(); public void startElement(
String namespaceURI,
String localName,
String qName,
Attributes attr)
throws SAXException
{ contents.reset(); } public void endElement(String namespaceURI, String localName, String qName)
throws SAXException
{ if (localName.equals("name"))
{
name = contents.toString();
} if (localName.equals("location"))
{
location = contents.toString();
} } public void characters(char[] ch, int start, int length) throws SAXException
{
contents.write(ch, start, length);
} public static void main(String[] argv)
{
System.out.println("Example2:");
try
{
System.out.println("No.1");
XMLReader xr = XMLReaderFactory.createXMLReader
// ("javax.xml.parsers.SAXParser");
("org.apache.xerces.parsers.SAXParser");
System.out.println("No.2");
Example2 ex2 = new Example2();
System.out.println("No.3");
xr.setContentHandler(ex2);
System.out.println("No.4");
xr.parse(new InputSource(new FileReader("c:\\Example2.xml")));
System.out.println("No.5");
System.out.println("Hello World from " + ex2.name + " in " + ex2.location);
}
catch (Exception e)
{
System.out.println("123");
e.printStackTrace();
System.out.println("456");
}
}}
-----------------------------
Example2:No.1No.2No.3No.4No.5Hello World from Bob in New York
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货