📄 operxml.java
字号:
package com.ddcc.utility;
/**
* <p>Title: 操作XML文件</p>
* <p>Description: 对XML文件进行读、写、修改操作</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: sinosoft</p>
* <p>CreateDate :2004-06-15</p>
* @author baico
* @version 1.0
*/
import java.util.List;
import java.net.*;
import java.util.*;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class OperXML {
private boolean blHasParent = false;
private String strParentTag = "";
private String strParentValue = "";
private Document doc = null;
private Element root = null;
/**
* 默认的构造函数
*/
public OperXML() {
}
/**
* 带参数的构造函数
* @param iParentTag 父节点的标签
* @param iParentValue 父节点的值
*/
public OperXML(String iParentTag,String iParentValue) {
this.blHasParent = true;
this.strParentTag = iParentTag;
this.strParentValue = iParentValue;
}
/**
* 解析XML文件
* @param iFileSrc 要解析的XML文件路径
* @throws IOException
* @throws JDOMException
* @throws Exception
*/
public void parser(String iFileSrc) throws IOException,JDOMException,Exception
{
try
{
SAXBuilder sb = new SAXBuilder();
doc = sb.build(new FileInputStream(iFileSrc));
root = doc.getRootElement();
}
catch(IOException ioe)
{
throw new IOException("解析不到此文件:" + iFileSrc);
}
catch(JDOMException jdome)
{
throw jdome;
}
catch(Exception e)
{
throw e;
}
}
/**
* 解析XML字符串
* @param iXMLStr 要解析的XML字符串
* @throws IOException
* @throws JDOMException
* @throws Exception
*/
public void parserFromXMLString(String iXMLStr) throws IOException,JDOMException,Exception
{
try
{
SAXBuilder sb = new SAXBuilder();
doc = sb.build(new StringReader(iXMLStr));
root = doc.getRootElement();
}
catch(IOException ioe)
{
throw ioe;
}
catch(JDOMException jdome)
{
jdome = new JDOMException("不是有效的XML文档!");
throw jdome;
}
catch(Exception e)
{
throw e;
}
}
/**
* 判断是否为XML文件格式
* @throws JDOMException
* @throws Exception
* @return 是/否 boolean
*/
public boolean isXMLFormat() throws JDOMException,Exception
{
boolean blFlag = false;
try
{
blFlag = doc.hasRootElement();
}
catch(Exception e)
{
throw e;
}
return blFlag;
}
/**
* 获取(根节点下)标签为iKeyCode的节点的个数
* @param iKeyCode 节点标签
* @return int 节点个数
* @throws JDOMException
* @throws Exception
*/
public int getCountByTag(String iKeyCode) throws JDOMException,Exception
{
int iCount = 0;
List lKeyList = null;
try
{
lKeyList = root.getChildren(iKeyCode);
if(lKeyList.size()>0)
{
iCount = lKeyList.size();
}
else
{
iCount = 0;
}
}
catch(Exception e)
{
throw e;
}
return iCount;
}
/**
* 获取(节点iElem下)标签为iKeyCode的节点的个数
* @param iKeyCode 节点标签
* @param iElem 节点Element
* @return int 节点个数
* @throws JDOMException
* @throws Exception
*/
public int getCountByTag(Element iElem,String iKeyCode) throws JDOMException,Exception
{
int iCount = 0;
List lKeyList = null;
try
{
lKeyList = iElem.getChildren(iKeyCode);
if(lKeyList.size()>0)
{
iCount = lKeyList.size();
}
else
{
iCount = 0;
}
}
catch(Exception e)
{
throw e;
}
return iCount;
}
/**
* 获取根节点的下级子节点的个数
* @return int 节点个数
* @throws JDOMException
* @throws Exception
*/
public int getCount() throws JDOMException,Exception
{
int iCount = 0;
List lKeyList = null;
try
{
lKeyList = root.getChildren();
if(lKeyList.size()>0)
{
iCount = lKeyList.size();
}
else
{
iCount = 0;
}
}
catch(Exception e)
{
throw e;
}
return iCount;
}
/**
* 获取节点iElem的下级子节点的个数
* @param iElem 节点Element
* @return int 节点个数
* @throws JDOMException
* @throws Exception
*/
public int getCount(Element iElem) throws JDOMException,Exception
{
int iCount = 0;
List lKeyList = null;
try
{
lKeyList = iElem.getChildren();
if(lKeyList.size()>0)
{
iCount = lKeyList.size();
}
else
{
iCount = 0;
}
}
catch(Exception e)
{
throw e;
}
return iCount;
}
/**
* 获取(根节点下)标签为iKeyCode的所有节点的值
* @param iKeyCode 节点标签
* @return String[] 节点的值
* @throws JDOMException
* @throws Exception
*/
public String[] getKeyValues(String iKeyCode) throws JDOMException,Exception
{
String[] strArrValue = null;
List lKeyList = null;
try
{
lKeyList = root.getChildren(iKeyCode);
if(lKeyList.size()>0)
{
strArrValue = new String[lKeyList.size()];
for(int i=0;i<lKeyList.size();i++)
{
Element item = (Element)lKeyList.get(i);
strArrValue[i] = item.getText();
}
}
else
{
throw new Exception("根下没有此节点:" + iKeyCode);
}
}
catch(Exception e)
{
throw e;
}
return strArrValue;
}
/**
* 获取(节点iElem下)标签为iKeyCode的所有节点的值
* @param iKeyCode 节点标签
* @param iElem 节点Element
* @return String[] 节点的值
* @throws JDOMException
* @throws Exception
*/
public String[] getKeyValues(Element iElem,String iKeyCode) throws JDOMException,Exception
{
String[] strArrValue = null;
List lKeyList = null;
try
{
lKeyList = iElem.getChildren(iKeyCode);
if(lKeyList.size()>0)
{
strArrValue = new String[lKeyList.size()];
for(int i=0;i<lKeyList.size();i++)
{
Element item = (Element)lKeyList.get(i);
strArrValue[i] = item.getText();
}
}
else
{
throw new Exception("节点" + iElem.getName() + "下没有此子节点:" + iKeyCode);
}
}
catch(Exception e)
{
throw e;
}
return strArrValue;
}
/**
* 获取(根节点下)标签为iKeyCode的第一个节点的值
* @param iKeyCode 节点标签
* @return String 第一个节点的值
* @throws JDOMException
* @throws Exception
*/
public String getKeyValue(String iKeyCode) throws JDOMException,Exception
{
String strValue = null;
List lKeyList = null;
try
{
lKeyList = root.getChildren(iKeyCode);
if(lKeyList.size()>0)
{
Element item = (Element)lKeyList.get(0);
strValue = item.getText();
}
else
{
throw new Exception("根下没有此节点:" + iKeyCode);
}
}
catch(Exception e)
{
throw e;
}
return strValue;
}
/**
* 获取(根节点下)标签为iKeyCode的第index+1个节点的值
* @param iKeyCode 节点标签
* @param index 节点的下标
* @return String 第index+1个节点的值
* @throws JDOMException
* @throws Exception
*/
public String getKeyValue(String iKeyCode,int index) throws JDOMException,Exception
{
String strValue = null;
List lKeyList = null;
try
{
lKeyList = root.getChildren(iKeyCode);
if(lKeyList.size()>0)
{
if(index>=lKeyList.size())
throw new Exception("下标越界:" + index);
else
{
Element item = (Element)lKeyList.get(index);
strValue = item.getText();
}
}
else
{
throw new Exception("根下没有此节点:" + iKeyCode);
}
}
catch(Exception e)
{
throw e;
}
return strValue;
}
/**
* 获取(节点iElem下)标签为iKeyCode的第一个节点的值
* @param iKeyCode 节点标签
* @param iElem 节点Element
* @return String 第一个节点的值
* @throws JDOMException
* @throws Exception
*/
public String getKeyValue(Element iElem,String iKeyCode) throws JDOMException,Exception
{
String strValue = null;
List lKeyList = null;
try
{
lKeyList = iElem.getChildren(iKeyCode);
if(lKeyList.size()>0)
{
Element item = (Element)lKeyList.get(0);
strValue = item.getText();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -