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

📄 xmlparser.java

📁 陕西电信sp客户端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// ----------------------------------------------------------------------------
// $Source: /cvs/vas2006/webpro2/webpro_java/src/com/onewaveinc/portalman/webpro/configure/XmlParser.java,v $
// ----------------------------------------------------------------------------
// Copyright (c) 2002 by Onewave Inc.
// ----------------------------------------------------------------------------
// $Id: XmlParser.java,v 1.1.1.1 2006/08/01 05:49:33 zhengx Exp $
// ----------------------------------------------------------------------------
// $Log: XmlParser.java,v $
// Revision 1.1.1.1  2006/08/01 05:49:33  zhengx
// no message
//
// Revision 1.1  2006/06/02 03:33:16  wuyan
// *** empty log message ***
//
// Revision 1.1  2005/12/08 10:36:50  like
// no message
//
// Revision 1.1  2003/07/28 06:31:17  zengc
// no message
//
// Revision 1.2  2002/09/02 09:38:38  zengc
// WebPro java Client  V1.2
//
// Revision 1.1  2002/08/09 08:49:44  zengc
// WebPro Configure Tools
//
// ----------------------------------------------------------------------------
package com.onewaveinc.portalman.webpro.configure;


/**
 * <p>Title: PortalMAN SDK API Documentation</p>
 * <p>Description: OneWave Technologies., Inc. PortalMAN Value-add Management Platform 3rd Software Development Kit</p>
 * <p>Copyright: Copyright (c) 2002 </p>
 * <p>Company: OneWave Technologies., Inc.</p>
 * @author 3rd AAA & ICP Integration Developement Team
 * @version 1.5
 */

import java.io.*;
import java.util.Properties;
import java.util.Enumeration ;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.w3c.dom.*;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

/**
 * 类XmlParser提供了对xml文件进行解析的工具
 */

public class XmlParser
{
	public char symbol = '.';
	protected Document document;
	protected String filename;
	private Properties props=null;
	private String defaultEncoding="GB2312";
	private boolean indenting;


	/**
	 * 构造函数
	 */
	public XmlParser() throws ConfigureFileException
	{
		document = this.createDocument();
	}

	/**
	 *构造函数  从文件获得dom
	 */
	public XmlParser(String fileName) throws ConfigureFileException
	{
		filename = fileName;
		document = this.load(fileName) ;
	}

	/**
	 * 直接获得dom
	 */
	public XmlParser(Document aDoc)
	{
		document = aDoc;
	}

	/**
	 * 一个xml变成string后转换成dom对象
	 */
	public XmlParser(String xmlcontent,String temp)throws ConfigureFileException
	{
		System.out.println(xmlcontent);
		document = this.translate(xmlcontent);
	}


	public Document translate(String xmlcontent) throws ConfigureFileException
	{
		Document doc=null;
		try
		{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder=factory.newDocumentBuilder();
			StringReader sr= new StringReader(xmlcontent);
			org.xml.sax.InputSource inputsource = new org.xml.sax.InputSource(sr);
			doc=docBuilder.parse(inputsource);
		}
		catch(Exception e)
		{
			throw new ConfigureFileException("XML_FILE_NOT_FOUND",xmlcontent);
		}
	   return doc;
	}




	/**
	* 产生一个新的Document对象
	*/
	public Document createDocument() throws ConfigureFileException
	{
		Document document=null;
		try
		{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder=factory.newDocumentBuilder();
			document = docBuilder.newDocument();
		}
		catch(Exception e)
		{
			throw new ConfigureFileException(e.toString());
		}
		return document;
	}

	/**
	*读取String参数
	*/
	public String getAttributeValue(String attrUri) throws ConfigureFileException
	{
		String attrName=null;
		String attrValue = null;
		int xmlLevel = getXmlLevel(attrUri);
		String allTagName[] = getAllTagName(attrUri);
		attrName = allTagName[xmlLevel-1];
		Node node =null;
		NodeList nodeList = document.getElementsByTagName("*") ;
		int nodeCount = nodeList.getLength();
		for(int j = 0;j<allTagName.length -1;j++)
		{
			for(int i = 0;i<nodeCount;i++)
			{
				node = nodeList.item(i) ;
				String a = node.getNodeName();
				if (allTagName[j].equalsIgnoreCase(node.getNodeName()))
				{
					nodeList = node.getChildNodes() ;
					nodeCount = nodeList.getLength() ;
					break;
				}
				if ((i==nodeCount-1) && (!allTagName[j].equalsIgnoreCase(node.getNodeName())))
				{
					throw new ConfigureFileException("XML_TAG_NOT_FOUND",attrUri);
				}
			}
		}

		NamedNodeMap namedNodeMap = node.getAttributes() ;
		Node mynode = namedNodeMap.getNamedItem(attrName);
		if(mynode==null)
		{
		throw new ConfigureFileException("XML_TAG_ATTRIBUTE_NOT_FOUND"+attrUri);
		}
		attrValue = mynode.getNodeValue() ;
		return  attrValue;
	}



	/**
	 *得到元素节点的text
	 *如<ele>eletext</ele>,得到的是eletext字符串.
	 *<code>add by bruce bian </code>
	 */
   public  String getNodeContent(Node n)
	{
		String val=null;
		if(n.getNodeType()!=Node.ELEMENT_NODE)
			  return val;
		NodeList childArr=n.getChildNodes();
		if(childArr==null)
				  val=null;
		else{
			 for(int i=0;i<childArr.getLength();i++){
		  Node child=childArr.item(i);
		  int type=child.getNodeType();
		  switch(type){
				case Node.TEXT_NODE :
				 val=child.getNodeValue();
				break;
			 }
		  }//end of for
			}//end of else
		 return val;
	}

	/**
	* 如果输入的是节点的标识,如“Domain.WebServer”,返回的是该节点;
	* 如果输入的是参数的标识,如“Domain.WebServer.EventsEnabled”,返
	* 回的是该参数所在的节点,是“Domain.WebServer”节点。
	*/
	public Node getNode(String nodeUri) throws ConfigureFileException
	{
		String allTagName[] = getAllTagName(nodeUri);
		Node node =null;
		NodeList nodeList = document.getElementsByTagName("*") ;
		int nodeCount = nodeList.getLength();
		for(int j = 0;j<allTagName.length ;j++)
		{
			for(int i = 0;i<nodeCount;i++)
			{
				node = nodeList.item(i) ;
				String a = node.getNodeName();
				if (allTagName[j].equalsIgnoreCase(node.getNodeName()))
				{
					nodeList = node.getChildNodes() ;
					nodeCount = nodeList.getLength() ;
					break;
				}
				if ((i==nodeCount-1) && (!allTagName[j].equalsIgnoreCase(node.getNodeName())))
				{
					throw new ConfigureFileException("XML_TAG_NOT_FOUND"+nodeUri);
				}
			}
		}
		String aaaa = node.getNodeName() ;
		return node;
	}

	/**
	*新建一个节点,参数为:新建节点所在的父节点位置,节点名
	*/
	public void addNode(String parentNodeIdent,String nodeIdent) throws ConfigureFileException
	{
		Node node;
		if(parentNodeIdent.length() == 0)
		{
			node=document;
		}
		else
		{
			node = getNode(parentNodeIdent);
			String abc = node.getNodeName() ;
		}
		Element ele = document.createElement(nodeIdent) ;
		String abcd = ele.getNodeName() ;
		node.appendChild(ele);
		//System.out.println("add node:\""+parentNodeIdent+"."+nodeIdent+"\",and mission complete!");
	}


	/**
	* 得到document中所有名字为tag的节点,返回一个NodeList类型的对象。
	*/
	// 这个NodeList对象和node.getChildNode()得到的对象是有区别的,
	// 这里的NodeList对象是不包含名字为“#text”的node的,
	// 而node.getChildNode()得到的NodeList对象在0、2、4的位置包含了“#text”的node。
	public NodeList getAllEntityByTagName(String tag)
	{
		Document doc = document;
		return doc.getElementsByTagName(tag);
	}





	/**
	 * 使Document对象具体的与某一个文件相对应。
	 */
	public Document load(String sFileName) throws ConfigureFileException
	{
		Document doc=null;
		try
		{
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder=factory.newDocumentBuilder();
			File f=new File(sFileName);
			doc=docBuilder.parse(f);
		}
		catch(SAXException ee)
		{
			throw new ConfigureFileException("XML_FILE_PARSER_ERROR",sFileName);
		}
		catch(IOException eee)
		{
			throw new ConfigureFileException("XML_FILE_IO_ERROR",sFileName);
		}
		catch(Exception e)
		{
			throw new ConfigureFileException("XML_FILE_NOT_FOUND",sFileName);
		}
		return doc;
	}



	/**
	 * reload 重新使Document对象具体的与某一个文件相对应。
	 * @param sFileName xml 文件名称.
	 */
	public void reload(String sFileName) throws ConfigureFileException
	{
		setXmlFile(sFileName);
		document=load(sFileName);
	}



	/**
	*根据encoding来保存到指定的文件中
	*/
	public void saveAs(String fileName,String encoding)
	{
		/*    try
		{
		   // XmlDocument xdoc = (XmlDocument) document;
		   // FileWriter fw=new FileWriter(fileName);
		   // xdoc.write(fw,encoding);
		   // fw.close() ;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		*/
	}

	/**
	*根据指定的encoding来保存文件
	*/
	public void save(String encoding)
	{
		/*    try
		{
		  //  XmlDocument xdoc = (XmlDocument) document;
		  //  FileWriter fw=new FileWriter(filename);
		  //  xdoc.write(fw,encoding);
		  //  fw.close() ;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		*/
	}

	/**
	*存为默认的gb2312编码的文件
	*/
	public void save() throws  ConfigureFileException
	{

	  try{
		FileWriter fileWriter=new FileWriter(filename);
		OutputFormat    format  = new OutputFormat(document,"gb2312",true);
	   //OutputFormat format= new OutputFormat(doc);
		XMLSerializer    serial = new XMLSerializer( fileWriter, format );
		serial.asDOMSerializer();
		serial.serialize( document.getDocumentElement() );
		}catch(Exception e)
		{
		   throw new ConfigureFileException(e.toString());
		}

	}

	/**
	* 得到节点或者参数的层次数目。
	*/
	public int getXmlLevel(String uri)
	{
		int count = 1;
		String subString = uri;
		int nextIndex = uri.indexOf(symbol) ;
		int stringLength = uri.length() ;
		while(nextIndex!=-1)
		{
			count++;
			subString = subString.substring(nextIndex+1,stringLength) ;
			stringLength = stringLength-nextIndex-1;
			nextIndex = subString.indexOf(symbol) ;
		}
		return count;
	}

	/**
	* 得到节点或者参数的各层次的名字。
	*/
	public String[] getAllTagName (String attrUri)
	{
		int xmlLevel = getXmlLevel(attrUri);
		String allTagName[] = new String [xmlLevel];

		String subString = attrUri;
		int nextIndex = attrUri.indexOf(symbol) ;
		int stringLength = attrUri.length() ;
		for(int i = 0;i < xmlLevel;i++)
		{
			if (i!=xmlLevel-1)
			{
				allTagName[i] = subString.substring(0,nextIndex);
				subString = subString.substring(nextIndex+1,stringLength) ;
				stringLength = stringLength-nextIndex-1;
				nextIndex = subString.indexOf(symbol) ;
			}
			else
			{
				allTagName[i] = subString.substring(0,stringLength) ;
			}
		}
		return allTagName;
	}

	/**
	*删除节点,nodeIdent形式为"Domain.WebServer"
	*/
	public void removeNode(String nodeUri) throws ConfigureFileException
	{
		Node node=null;
		Node parentNode=null;
		node=getNode(nodeUri);
		if(node!=null)
		{
			parentNode=node.getParentNode() ;
			parentNode.removeChild(node);
			//  System.out.println("remove node:\""+nodeUri+"\",and mission complete!");
		}
		else
		{
			throw new ConfigureFileException("XML_TAG_NOT_FOUND",nodeUri);
		}
	}

	/**
	*检查node是否存在,存在返回true,不存在返回false
	*/
	public boolean nodeExist(String nodeUri) throws ConfigureFileException
	{
	   Node node=null;
	  try{
			node = getNode(nodeUri);
			return true;
	  }catch(ConfigureFileException cfe){
		  return false;
	  }
	}

	/**
	*检查参数是否存在,存在返回true,不存在返回false
	*/
	public boolean attrExist(String attrUri) throws ConfigureFileException
	{
		String allTagName[] = getAllTagName(attrUri) ;

⌨️ 快捷键说明

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