Home  >  Article  >  Backend Development  >  XML parsing sax parsing case (1) Read the contact.xml file and completely output the document content

XML parsing sax parsing case (1) Read the contact.xml file and completely output the document content

黄舟
黄舟Original
2017-02-16 15:19:102074browse

1. Create a new Demo2 class:


import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * 读取contact.xml文件,完整输出文档内容
 * @author APPle
 *
 */
public class Demo2 {

	public static void main(String[] args)throws Exception {
		//1.创建SAXParser
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		//2.读取xml文件
		MyDefaultHandler2 handler = new MyDefaultHandler2();
		parser.parse(new File("./src/contact.xml"), handler);
		String content = handler.getContent();
		System.out.println(content);
	}

}


2. Create a custom MyDefaulthander2


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
 * SAX处理器程序
 * @author APPle
 */
public class MyDefaultHandler2 extends DefaultHandler {
	//存储xml文档信息
	private StringBuffer sb = new StringBuffer();
	
	//获取xml信息
	public String getContent(){
		return sb.toString();
	}
	

	/**
	 * 开始标签
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		sb.append("<"+qName);
		//判断是否有属性
		if(attributes!=null){
			for(int i=0;i<attributes.getLength();i++){//int getLength() 返回此列表中的属性个数。 
				//得到属性名称   id="001"
				String attrName = attributes.getQName(i);//String getQName(int index) 通过索引查找属性的 XML 限定(前缀)名。 
				//得到属性值
				String attrValue = attributes.getValue(i);//String getValue(int index) 通过索引查找属性的值。 
				sb.append(" "+attrName+"=\""+attrValue+"\"");
			}
		}
		sb.append(">");
	}
	
	/**
	 * 文本内容
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		//得到当前读取的文本
		String content = new String(ch,start,length);
		sb.append(content);
	}
	
	/**
	 * 结束标签
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		sb.append("</"+qName+">");
	}
}

Print output result:

<contactList>
    <contact id="001" name="eric">
        <name>张三</name>
        <age>20</age>
        <phone>134222223333</phone>
        <email>zhangsan@qq.com</email>
        <qq>432221111</qq>
    </contact>
    <contact id="002" name="jacky">
        <name>eric</name>
        <age>20</age>
        <phone>134222225555</phone>
        <email>lisi@qq.com</email>
        <qq>432222222</qq>
    </contact>
</contactList>

The above is the sax parsing case of XML parsing (1) Read the contact.xml file and completely output the document content. For more related content, please pay attention to the PHP Chinese website (www.php. cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn