<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="log.dir" value="dir" /> <property name="bufferedIO" value="false" /> <property name="immediateFlush" value="true" /> <property name="encoding" value="UTF-8" /> <property name="append" value="true" /> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>patter</Pattern> </layout> </appender> <logger name="logger"> <level value="DEBUG" /> <appender-ref ref="FILE" /> </logger> <root> <level value="INFO" /> <appender-ref ref="FILE" /> </root> </configuration>
解析上述文档的实例代码:
import java.io.FileReader;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* 重载DefaultHandler类的方法
* 以拦截SAX事件通知。
* 关于所有有效事件,见org.xml.sax.ContentHandler
* @author Administrator
*
*/
public class XmlReaderTest extends DefaultHandler {
public void startDocument() throws SAXException {
System.out.println("解析文档开始");
}
public void endDocument() throws SAXException {
System.out.println("解析文档结束");
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes attr) throws SAXException {
System.out.print("\r\n<"+qName);
if(attr != null && attr.getLength() > 0){//节点的属性
for(int i=0; i<attr.getLength(); i++){
System.out.print(" "+attr.getQName(i)+"='"+attr.getValue(i)+"'");
}
}
System.out.print(">");
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
System.out.print("</" + qName + ">\r\n");
}
public void characters(char[] ch, int start, int length)
throws SAXException {
String value = new String(ch, start, length).trim();
System.out.print(value);
}
public static void main(String[] argv) {
try {
SAXParserFactory spFactory = SAXParserFactory.newInstance();
SAXParser sParser = spFactory.newSAXParser();
sParser.parse(new InputSource(new FileReader("c://test.xml")), new XmlReaderTest());
// 建立SAX 2解析器...
// XMLReader xr = XMLReaderFactory
// .createXMLReader("org.apache.xerces.parsers.SAXParser");
// 安装ContentHandler...
//xr.setContentHandler(new XmlReaderTest());
// 解析文件...
//xr.parse(new InputSource(new FileReader("test.xml")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出为:
解析文档开始
<configuration>
<property name='log.dir' value='dir'></property>
<property name='bufferedIO' value='false'></property>
<property name='immediateFlush' value='true'></property>
<property name='encoding' value='UTF-8'></property>
<property name='append' value='true'></property>
<appender name='CONSOLE' class='ch.qos.logback.core.ConsoleAppender'>
<layout class='ch.qos.logback.classic.PatternLayout'>
<Pattern>patter</Pattern>
</layout>
</appender>
<logger name='logger'>
<level value='DEBUG'></level>
<appender-ref ref='FILE'></appender-ref>
</logger>
<root>
<level value='INFO'></level>
<appender-ref ref='FILE'></appender-ref>
</root>
</configuration>
解析文档结束