[android]-xml解析示便-SAX

支持任意级深度的xml,通过taglist.get(length)来获取结点值,通过taglist.size()来获取xml结点的深度

package com.nt.demo.xml;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ORDER_Product_Handler extends DefaultHandler {

	public StringBuffer sb_text = new StringBuffer();
	private List<String> taglist = new ArrayList<String>();

	public List<String> attach = new ArrayList<String>();
	public List<String> attachments = new ArrayList<String>();

	public void startDocument() throws SAXException {
		System.out.println("-------order xml begin----------");

		// ------gen html------
		sb_text.append("<html><head></head><style type=\"text/css\">body,td,th {font-size: 12px;}</style><body>");

		// ---------add on 2012.03.09----------
		sb_text.append("<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\">");
		sb_text.append("<tr bgcolor=\"#99CCFF\">");
		// sb_text.append("<td><a href=\"htzl\">订单资料</a></td>");
		// sb_text.append("<td bgcolor=\"#ffffff\">成本估算</td>");
		sb_text.append("<td><a href=\"order_ddzl\">订单资料</a></td>");
		sb_text.append("<td bgcolor=\"#ffffff\">产品资料</td>");

		sb_text.append("</tr>");
		sb_text.append("</table>");
		// ------------------------------

		sb_text.append("<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\">");
	}

	public void endDocument() throws SAXException {
		System.out.println("----------order xml end---------");

		// ------gen html------
		sb_text.append("</table><br/>");
		sb_text.append("</body></html>");

	}

	public void startElement(String namespaceURI, String localName,
			String qName, Attributes attr) throws SAXException {

		System.out.println("start element:" + localName);
		// 把tag加到taglist中以便使用
		taglist.add(localName);

		//
		System.out.println(String.format("taglist.size():%s,localName:%s",
				taglist.size(), localName));

		if (taglist.size() >= 2) {
			if (localName.equals("line")) {
				// <line Seq="1" ProcuctDesc="" ProcuctType="" Vender=""
				// Price="" Quantity="" Psum="" Mdate1="" Mdate2="" MPeriod=""/>
				// 判断父节点标签
				// 获取标签的全部属性
				for (int i = 0; i < attr.getLength(); i++) {
					System.out.println(attr.getLocalName(i) + "="
							+ attr.getValue(i));

					sb_text.append("<tr><td bgcolor=\"#99CCFF\">&nbsp;");
					// sb_text.append(attr.getLocalName(i));
					if (attr.getLocalName(i).equalsIgnoreCase("Seq")) {
						sb_text.append("序号");
					} else if (attr.getLocalName(i).equalsIgnoreCase(
							"ProcuctDesc")) {
						sb_text.append("产品摘要");
					} else if (attr.getLocalName(i).equalsIgnoreCase(
							"ProcuctType")) {
						sb_text.append("类别");
					} else if (attr.getLocalName(i).equalsIgnoreCase("Vender")) {
						sb_text.append("生产商");
					} else if (attr.getLocalName(i).equalsIgnoreCase("Price")) {
						sb_text.append("销售价格");
					} else if (attr.getLocalName(i)
							.equalsIgnoreCase("Quantity")) {
						sb_text.append("数量");
					} else if (attr.getLocalName(i).equalsIgnoreCase("Psum")) {
						sb_text.append("金额");
					} else if (attr.getLocalName(i).equalsIgnoreCase("Mdate1")) {
						sb_text.append("维护开始日期");
					} else if (attr.getLocalName(i).equalsIgnoreCase("Mdate2")) {
						sb_text.append("维护结束日期");
					} else if (attr.getLocalName(i).equalsIgnoreCase("MPeriod")) {
						sb_text.append("维护期");
					}

					sb_text.append("</td><td bgcolor=\"#CCCCCC\">&nbsp;");
					sb_text.append(attr.getValue(i));
					sb_text.append("</td></tr>");
				}
				sb_text.append("<tr><td bgcolor=\"#99CCFF\" colspan=\"2\">&nbsp;--------------</td></tr>");

			}
			// --------------

		}
		// ------gen html------
		// sb_text.append(localName);
		// sb_text.append(":");

	}

	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		System.out.println("end element:" + taglist.get(taglist.size() - 1));
		taglist.remove(taglist.size() - 1);// 移除最先放到list中的元素

		if (localName.equals("r")) {
			this.printout();
		}

		// ------gen html------
		// sb_text.append("<br/>");
		if (localName.equalsIgnoreCase("line")) {
			sb_text.append("</td></tr>");
		}

	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {

		String curtagname = taglist.get(taglist.size() - 1);

		String elementtext = new String(ch, start, length);
		String output = String.format("tag[%s]:%s",
				taglist.get(taglist.size() - 1), elementtext);
		System.out.println(output);

		// ------gen html------
		if (curtagname.equalsIgnoreCase("line")) {
			sb_text.append(elementtext);
		}

		// -----------------------------

	}

	private void printout() {
		System.out.print("output:");
		System.out.println();
		System.out.print(sb_text.toString());
		System.out.println();
	}

}

你可能感兴趣的:(java,xml,android,sax)