菜鸟的迷惑_3:SAX解析

moumouren 2002-03-06 04:53:15
大虾们能否提供用sun的SAX解析器,解析xml文档的例子?
...全文
111 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
vcvj 2002-09-23
  • 打赏
  • 举报
回复
sc
oliff 2002-03-13
  • 打赏
  • 举报
回复
上面的程序需要xerces parser.(http://xml.apache.org)

output
========================
D:\JDOM_study>java SurveyReader
Tallying survey results...
Start element: surveys
Start element: response
username: bob
Start element: question
subject: appearance
End Element: question
Start element: question
subject: communication
End Element: question
Start element: question
subject: ship
End Element: question
Start element: question
subject: inside
End Element: question
Start element: question
subject: implant
End Element: question
End Element: response
Start element: response
username: sue
Start element: question
subject: appearance
End Element: question
Start element: question
subject: communication
End Element: question
Start element: question
subject: ship
End Element: question
Start element: question
subject: inside
End Element: question
Start element: question
subject: implant
End Element: question
End Element: response
Start element: response
username: carol
Start element: question
subject: appearance
End Element: question
Start element: question
subject: communication
End Element: question
Start element: question
subject: ship
End Element: question
Start element: question
subject: inside
End Element: question
Start element: question
subject: implant
End Element: question
End Element: response
End Element: surveys
oliff 2002-03-13
  • 打赏
  • 举报
回复
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;




public class SurveyReader extends DefaultHandler
{

public SurveyReader() {
}
public void error (SAXParseException e) {
System.out.println("Error parsing the file: "+e.getMessage());
}

public void warning (SAXParseException e) {
System.out.println("Problem parsing the file: "+e.getMessage());
}

public void fatalError (SAXParseException e) {
System.out.println("Error parsing the file: "+e.getMessage());
System.out.println("Cannot continue.");
System.exit(1);
}
int indent = 0;
public void startDocument()
throws SAXException {
System.out.println(
"Tallying survey results...");
indent = -4;
}

public void printIndent(int indentSize) {
for (int s = 0; s < indentSize; s++) {
System.out.print(" ");
}
}

public void startElement(
String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
indent = indent + 4;
printIndent(indent);

System.out.print("Start element: ");
//System.out.println(localName);
System.out.println(qName);

for (int att = 0;
att < atts.getLength();
att++) {
printIndent(indent + 4);
String attName = atts.getQName(att);
System.out.println(" "
+ attName + ": "
+ atts.getValue(attName));
}

}

public void endElement(String namespaceURI,
String localName,
String qName)
throws SAXException {

printIndent(indent);
System.out.println("End Element: "+qName);
indent = indent - 4;
}

public static void main (String args[]) {

XMLReader xmlReader = null;

try {

SAXParserFactory spfactory =
SAXParserFactory.newInstance();

spfactory.setValidating(false);

SAXParser saxParser =
spfactory.newSAXParser();

xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new SurveyReader());
xmlReader.setErrorHandler(new SurveyReader());

InputSource source =
new InputSource("surveys.xml");
xmlReader.parse(source);

} catch (Exception e) {
System.err.println(e);
System.exit(1);
}
}
}

=================================
surveys.xml

<?xml version="1.0"?>
<surveys>
<response username="bob">
<question subject="appearance">A</question>
<question subject="communication">B</question>
<question subject="ship">A</question>
<question subject="inside">D</question>
<question subject="implant">B</question>
</response>
<response username="sue">
<question subject="appearance">C</question>
<question subject="communication">A</question>
<question subject="ship">A</question>
<question subject="inside">D</question>
<question subject="implant">A</question>
</response>
<response username="carol">
<question subject="appearance">A</question>
<question subject="communication">C</question>
<question subject="ship">A</question>
<question subject="inside">D</question>
<question subject="implant">C</question>
</response>
</surveys>
moumouren 2002-03-13
  • 打赏
  • 举报
回复
我理解楼上所说的概念,我想要的是一个实际的例子,越简单越好。
麻烦各位大虾了!
王鹏云 2002-03-06
  • 打赏
  • 举报
回复
关键要分清楚sax与dom的区别,dom是解析为树状模型,而sax是事件驱动的。
mirrorsite 2002-03-06
  • 打赏
  • 举报
回复
parse
public void parse(InputSource source)
throws SAXException,
IOExceptionDeprecated.
Parse an XML document.
The application can use this method to instruct the SAX parser to begin parsing an XML document from any valid input source (a character stream, a byte stream, or a URI).

Applications may not invoke this method while a parse is in progress (they should create a new Parser instead for each additional XML document). Once a parse is complete, an application may reuse the same Parser object, possibly with a different input source.


Parameters:
source - The input source for the top-level of the XML document.
Throws:
SAXException - Any SAX exception, possibly wrapping another exception.
IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
See Also:
InputSource, parse(java.lang.String), setEntityResolver(org.xml.sax.EntityResolver), setDTDHandler(org.xml.sax.DTDHandler), setDocumentHandler(org.xml.sax.DocumentHandler), setErrorHandler(org.xml.sax.ErrorHandler)

--------------------------------------------------------------------------------

parse
public void parse(String systemId)
throws SAXException,
IOExceptionDeprecated.
Parse an XML document from a system identifier (URI).
This method is a shortcut for the common case of reading a document from a system identifier. It is the exact equivalent of the following:

parse(new InputSource(systemId));
If the system identifier is a URL, it must be fully resolved by the application before it is passed to the parser.


Parameters:
systemId - The system identifier (URI).
Throws:
SAXException - Any SAX exception, possibly wrapping another exception.
IOException - An IO exception from the parser, possibly from a byte stream or character stream supplied by the application.
See Also:
parse(org.xml.sax.InputSource)
poney 2002-03-06
  • 打赏
  • 举报
回复
请问Msdom怎么用呢?
ChinaOk 2002-03-06
  • 打赏
  • 举报
回复
没用过。去java版问问也许可以。
我一直用msdom呢。

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧