⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rootelementgenerator.java

📁 用于串口通讯测试的工具软件。完全使用java语言编写。
💻 JAVA
字号:
package com.zcsoft.xml;

import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;

/**
 * 构建XML文档的的根对象。
 * 构建完毕后,形成一个com.zcsoft.xml.Element根对象。
 *
 * @see #parceXml
 *
 * @author 蒋智湘
 * @version 1.0
 */
public class RootElementGenerator extends HandlerBase
{
	protected Locator locator;
	protected String text;
	protected Element rootElement;
	protected Stack elementsStack = new Stack();

	protected RootElementGenerator(){}

	/**
	 *
	 * @param xmlFile XML文档文件
	 * @return XML文档的根元素对象
	 * @throws Exception
	 */
	public static Element fromXml(File xmlFile) throws Exception
	{
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setNamespaceAware(false);
			factory.setValidating(false);
//			factory.setFeature("http://xml.org/sax/features/validatidon", true);
			SAXParser parser = factory.newSAXParser();
			RootElementGenerator reg = new RootElementGenerator();
			parser.parse(xmlFile, reg);
			return reg.rootElement;
		}
		catch (SAXException ex)
		{
			if( ex.getException() != null ) throw ex.getException();
			else throw ex;
		}
	}


	public void startElement (String tag, AttributeList attributes)
			throws SAXException
	{
		Element newElement = new Element(tag);
		if (elementsStack.empty())
		{
			this.rootElement = newElement;
		}
		int cntAttributes = attributes != null?attributes.getLength():0;
		if (cntAttributes > 0)
		{
			newElement.encureAttributesCount(cntAttributes);
			for (int i = 0; i < cntAttributes; i++)
			{
				newElement.addAttibute(new Attribute(attributes.getName(i), attributes.getValue(i)));
			}
		}
		if ( !"".equals(text) )
		{
			if ( !this.elementsStack.empty() )
			{
				Element parent = (Element)this.elementsStack.peek();
				Element textElement = new Element(Element.TAG_TEXT);
				textElement.setTagValue(text);
				parent.addChild(textElement);
			}
		}
		this.elementsStack.push(newElement);
		text = "";
	}


	public void endElement (String tag) throws SAXException
	{
		Element e = (Element)this.elementsStack.pop();
		if ( !this.elementsStack.empty() )//说明不是根节点的结束
		{
			Element parent = (Element)this.elementsStack.peek();
			parent.addChild(e);
		}
		if ( !"".equals(text) )//说明起止节点间存在文本
		{
			Element textElement = new Element(Element.TAG_TEXT);
			textElement.setTagValue(text);
			e.addChild(textElement);
		}
		text = "";
	}
//
//	public String positionToString()
//	{
//		StringBuffer sb = new StringBuffer();
//		if( locator!=null ) sb.append("Line ").append(locator.getLineNumber()).append(" ");
//		if( this.latestElement != null )
//		{
//			sb.append(this.latestElement);
//			for (int i = 0; i < this.latestElement.getAttributesCount (); i++)
//			{
//				sb.append(this.latestElement.getAttribute(i));
//				sb.append(" ");
//			}
//		}
//		return sb.toString();
//	}


	public void characters (char buf [], int offset, int len)
			throws SAXException
	{
		//考虑增加一个field,以便使用者设定是否做出如下处理
		//忽略为增加XML文档的可读性而添加的回车、换行和缩进字符
		int end = offset + len;
		char c;
		for (; offset < end; offset++)
		{
			c = buf[offset];
			if ( c != '\r' && c != '\n' && c != '\t')
			{
				break;
			}
		}
		if (offset < end)
		{
			text = text == ""?new String( buf, offset, len ):(text + new String( buf, offset, len ));
			//System.out.println(this.locator.getLineNumber() + " " + text);
		}
		//text += new String( buf, offset, len );
	}
//
//	public void ignorableWhitespace (char ch[], int start, int length)
//			throws SAXException
//	{
//		System.out.println(this.locator.getLineNumber());
//	}

	public void setDocumentLocator (Locator locator)
	{
		this.locator = locator;
	}

//	//debug
//	public static void main(String[] args)
//	{
//		try
//		{
//			System.out.println(RootElementGenerator.fromXml(new File("E:\\commAPI\\defaultRoot\\stock.xml")));
//		}
//		catch (Exception ex)
//		{
//			ex.printStackTrace();
//		}
//	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -