📄 xmlparser.java
字号:
String nodeUri = allTagName[0];
for(int i = 1; i<allTagName.length - 1;i++)
{
nodeUri = nodeUri.concat(".") .concat(allTagName[i]) ;
}
Node node = getNode(nodeUri);
if (node != null)
{
NamedNodeMap namedNodeMap = node.getAttributes() ;
Node myNode = namedNodeMap.getNamedItem(allTagName[allTagName.length -1]) ;
if(myNode!=null)
{
return true;
}
else
return false;
}
else
return false;
}
/**
*删除参数,例如attrIdent形式为“Domain.WebServer.EventsEnabled”,其中EventsEnabled为参数名,
*/
public void removeAttr(String attrUri) throws ConfigureFileException
{
String allTagName[] = getAllTagName(attrUri) ;
String nodeUri = allTagName[0];
for(int i = 1; i<allTagName.length - 1;i++)
{
nodeUri = nodeUri.concat(".") .concat(allTagName[i]) ;
}
Node node = getNode(nodeUri);
NamedNodeMap namedNodeMap = node.getAttributes() ;
namedNodeMap.removeNamedItem(allTagName[allTagName.length-1]) ;
}
/**
*修改参数的值
*/
public void modifyAttr(String attrUri,String newAttrValue) throws ConfigureFileException
{
String allTagName[] = getAllTagName(attrUri) ;
String nodeUri = allTagName[0];
for(int i = 1; i<allTagName.length - 1;i++)
{
nodeUri = nodeUri.concat(".") .concat(allTagName[i]) ;
}
Node node = getNode(nodeUri);
NamedNodeMap namedNodeMap = node.getAttributes() ;
Node myNode = namedNodeMap.getNamedItem(allTagName[allTagName.length-1]) ;
myNode.setNodeValue(newAttrValue) ;
}
/**
*新建一个参数,参数为:新建参数所在的节点位置,参数名,参数值
*/
public void addAttr(String nodeIdent,String attrName,String attrValue) throws ConfigureFileException
{
Node node = getNode(nodeIdent);
NamedNodeMap namedNodeMap = node.getAttributes() ;
Attr attr = document.createAttribute(attrName) ;
attr.setValue(attrValue) ;
namedNodeMap.setNamedItem(attr) ;
}
/**
*得到文件中所有的attributes的value,并且这些value保存在随同string[] 数组中
*/
public String [] getAllAttributeValues()
{
Properties properties = new Properties();
NodeList nodeList = document.getElementsByTagName("*") ;
int nodeListLength = nodeList.getLength() ;
String [] allValues=new String [nodeListLength];
String nodeName = "";
int k=0;
for (int i = 0;i<nodeListLength;i++)
{
Node node = nodeList.item(i) ;
nodeName = node.getNodeName().concat(".") ;
NamedNodeMap namedNodeMap = node.getAttributes() ;
int namedNodeMapLength = namedNodeMap.getLength() ;
for (int j = 0;j<namedNodeMapLength;j++)
{
Node attrNode = namedNodeMap.item(j) ;
String fatherNodeName = "";
String attrName = nodeName.concat(attrNode.getNodeName()) ;
String attrValue = attrNode.getNodeValue() ;
Node tempNode = node;
while(tempNode.getParentNode()!=null)
{
tempNode = tempNode.getParentNode();
String avdf = tempNode.getNodeName();
if(i==0)
{
fatherNodeName = tempNode.getNodeName();
}
else
fatherNodeName = tempNode.getNodeName().concat(".").concat(fatherNodeName) ;
}
String attrNodeNameAll = fatherNodeName.concat(nodeName.concat(attrNode.getNodeName())) ;
int length = attrNodeNameAll.length() ;
String attrNodeName = attrNodeNameAll.substring(9,length) ;
if(attrNodeName.startsWith(".") )
{
attrNodeName = attrNodeName.substring(1,length-9) ;
}
if(attrValue.substring(1,2).equals(":")){
allValues[k]=attrValue;
k=k+1;
}
}
}
return allValues;
}
/**
*得到文件中所有的attributes,并且这些attr保存在Properties对象中
*/
public Properties getAllProperties()
{
Properties properties = new Properties();
NodeList nodeList = document.getElementsByTagName("*") ;
int nodeListLength = nodeList.getLength() ;
// System.out.println("Length:"+nodeListLength) ;
String nodeName = "";
for (int i = 0;i<nodeListLength;i++)
{
Node node = nodeList.item(i) ;
nodeName = node.getNodeName().concat(".") ;
NamedNodeMap namedNodeMap = node.getAttributes() ;
int namedNodeMapLength = namedNodeMap.getLength() ;
for (int j = 0;j<namedNodeMapLength;j++)
{
Node attrNode = namedNodeMap.item(j) ;
String fatherNodeName = "";
String attrName = nodeName.concat(attrNode.getNodeName()) ;
String attrValue = attrNode.getNodeValue() ;
Node tempNode = node;
while(tempNode.getParentNode()!=null)
{
tempNode = tempNode.getParentNode();
String avdf = tempNode.getNodeName();
if(i==0)
{
fatherNodeName = tempNode.getNodeName();
}
else
fatherNodeName = tempNode.getNodeName().concat(".").concat(fatherNodeName) ;
}
//System.out.println("fatherNodeName is :"+fatherNodeName) ;//.concat(".")
String attrNodeNameAll = fatherNodeName.concat(nodeName.concat(attrNode.getNodeName())) ;
int length = attrNodeNameAll.length() ;
String attrNodeName = attrNodeNameAll.substring(9,length) ;
if(attrNodeName.startsWith(".") )
{
attrNodeName = attrNodeName.substring(1,length-9) ;
}
properties.setProperty(attrNodeName,attrValue) ;
//System.out.println(attrNodeName+"&"+attrValue+"&"+properties.getProperty(attrNodeName) ) ;
}
}
return properties;
}
/**
*读取节点,返回节点中所有的参数(不包括子节点的),并保存在Properties对象中
*/
public Properties getNodeProperties(String nodeIdent) throws ConfigureFileException
{
Properties properties = new Properties();
Node node = getNode(nodeIdent);
NamedNodeMap namedNodeMap = node.getAttributes() ;
int length = namedNodeMap.getLength() ;
for(int i = 0;i<length;i++)
{
Node myNode = namedNodeMap.item(i) ;
properties.setProperty(myNode.getNodeName() ,myNode.getNodeValue() ) ;
//properties.setProperty(nodeIdent.concat(".").concat(myNode.getNodeName()) ,myNode.getNodeValue() ) ;
//System.out.println(nodeIdent.concat(".").concat(myNode.getNodeName())+" & "+myNode.getNodeValue()) ;
}
return properties;
}
/**
* 得到指定节点的下一层节点的tagName
*/
public String[] getChildNodeTagName(String nodeUri) throws ConfigureFileException
{
String[] nodeTagName = getAllTagName(nodeUri);
int nodeLength = nodeTagName.length ;
Node node = getNode(nodeUri);
NodeList childNodeList = node.getChildNodes() ;
int childNodeLength = childNodeList.getLength() ;
//if (childNodeLength==0) return new String[0];
String[] childNodeTagName = new String[(childNodeLength-1)/2];
int j = 0;
for(int i = 0;i<childNodeLength;i++)
{
Node tempNode = childNodeList.item(i) ;
if(tempNode.getNodeName() .equalsIgnoreCase("#text") );
else
{
//System.out.println("nodename is :"+tempNode.getNodeName() ) ;
childNodeTagName[j++] = tempNode.getNodeName() ;
}
}
return childNodeTagName;
}
/**
* 使xml文件中的<Include FileName="xxx.xml">替换成xxx.xml的文件内容,
*/
public void makeInclude() throws ConfigureFileException
{
NodeList nodeList = document.getElementsByTagName("Include") ;
int nodeListlength = nodeList.getLength() ;
// System.out.println(nodeListlength) ;
for(int i = 0 ; i < nodeListlength ; i++)
{
Node node = nodeList.item(i) ;//Include节点
NamedNodeMap namedNodeMap = node.getAttributes() ;
Node attr = namedNodeMap.getNamedItem("FileName") ;
String fileName = attr.getNodeValue() ;
Document childDocument;
childDocument = new XmlParser().load(fileName) ;
Element ele = childDocument.getDocumentElement() ;//得到子Document的根节点Domain
Node oldNode = ele.cloneNode(true);//克隆子Document的根节点Domain
Node parentNode = node.getParentNode() ;
// parentNode.removeChild(node) ;
cloneNode(oldNode,parentNode);
}
for(int i = 0 ; i < nodeListlength ; i++)
{
Node node = nodeList.item(0) ;//Include节点
Node parentNode = node.getParentNode() ;
// System.out.println(node.getNodeName() +"@@"+parentNode.getNodeName() ) ;
parentNode.removeChild(node) ;
nodeList = document.getElementsByTagName("Include") ;
}
}
/**
* 把要Include的节点oldNode放到parentNode下面。
*/
private void cloneNode(Node oldNode,Node parentNewNode)
{
String nodeTag = oldNode.getNodeName() ;
if(nodeTag.equals("#text"))
{
}
else
{
Element ele = document.createElement(nodeTag) ;
Node childNode = parentNewNode.appendChild(ele) ;
NamedNodeMap namedOldNodeMap = oldNode.getAttributes() ;
NamedNodeMap namedNewNodeMap = childNode.getAttributes() ;
int length = namedOldNodeMap.getLength() ;
for(int i = 0;i<length;i++)
{
Node attrNode = namedOldNodeMap.item(i) ;
String attrNodeName = attrNode.getNodeName() ;
String attrNodeValue = attrNode.getNodeValue() ;
Attr attr = document.createAttribute(attrNodeName) ;
attr.setValue(attrNodeValue) ;
namedNewNodeMap.setNamedItem(attr) ;
}
if(oldNode.hasChildNodes() )
{
NodeList nodeList = oldNode.getChildNodes() ;
int childLength = nodeList.getLength() ;
for (int i = 0;i<childLength;i++)
{
Node oldNode1 = nodeList.item(i) ;
cloneNode(oldNode1,childNode);
}
}
}//end else
}
/**
* 根据attri的值得到她所在node的tag名字
*/
public String getAttrsTagName(String nodeUri,String value) throws ConfigureFileException
{
String tagName = null;
Node aNode = getNode(nodeUri);
NodeList aNodeList = aNode.getChildNodes();
int nodeListLength = aNodeList.getLength();
for(int i = 0 ; i < nodeListLength ; i++)
{
if (!aNodeList.item(i).getNodeName().equalsIgnoreCase("#text"))
{
Node aNodeChild = aNodeList.item(i);
NamedNodeMap aNamedNodeMap = aNodeChild.getAttributes();
int namedNodeMapLength = aNamedNodeMap.getLength();
for(int j = 0 ; j < namedNodeMapLength ; j ++)
{
if (aNamedNodeMap.item(j).getNodeValue().equalsIgnoreCase(value))
{
tagName = aNodeChild.getNodeName();
return tagName;
}
}
}
}
if(tagName==null)
{
throw new ConfigureFileException("XML_TAG_ATTRIBUTE_VALUE_NOT_FOUND",nodeUri+"/"+value);
}
return tagName;
}
/**
*得到document对象
*/
public Document getDocument()
{
return this.document ;
}
/**
* 设置uri的形式,有时候需要uri为"XX_XX_XX_XX",可以setSymbol('_')
*/
public void setSymbol(char ch){
symbol = ch;
}
/**
* 设置默认得编码
*/
public void setDefaultEncoding(String encoding)
{
defaultEncoding=encoding;
}
/**
*设置输出的xml 格式是否对齐缩进
*/
public void setDefaultIndent(boolean indenting)
{
this.indenting = indenting;
}
/**
* 设置XML 文件的名称
*/
public void setXmlFile(String xmlFile)
{
this.filename = xmlFile;
}
/**
* 得到DOM对象的String,并按照default来编码
*
*/
public String getDOMString() throws ConfigureFileException
{
org.apache.xml.serialize.OutputFormat format = new OutputFormat(document,defaultEncoding,indenting);
java.io.StringWriter writer = new StringWriter();
XMLSerializer serializer = new org.apache.xml.serialize.XMLSerializer(writer,format);
try{
serializer.asDOMSerializer();
serializer.serialize(document);
}catch(IOException e){
throw new ConfigureFileException(e.toString(),e);
}
String domStr = "";
domStr = writer.toString() ;
return domStr;
}
/**
*根据输入的Element 得到元素的attribute的值
*@param element the element node that constaint many attributes
*@param attribute the attribute you want to get the value
*/
public String getAttributeIgnoreCaseValue(Element element,String attribute)throws ConfigureFileException
{
String value="";
NamedNodeMap atts = element.getAttributes();
int nodeType;
for (int i = 0; i < atts.getLength(); i++) {
Node att = atts.item(i);
String val = att.getLocalName();
if (val != null){
if(val.equalsIgnoreCase(attribute)==true){
value=att.getNodeValue();
break;
}//end val.equalsIgnoreCase(attribute)
}//end if
}//end for(int i=0;i< ;i++)
return value;
}
/**
*根据输入的encoding 得到DOM对象的String
*@param encoding 要设置的encoding
*/
public String getDOMString(String encoding ) throws ConfigureFileException
{
setDefaultEncoding(encoding);
return this.getDOMString();
}
/**
* 得到DOM对象的String,并按照encoding来编码
* @ param encoding 编码格式
* @ param indenting If indent is true,the document will be pretty
* printed with the default indentation level and default line wrapping.
*/
public String getDOMString(String encoding,boolean indenting) throws ConfigureFileException
{
this.setDefaultEncoding(encoding);
this.setDefaultIndent(indenting);
return getDOMString();
}
public Properties getChildNode(String aTagName){
Properties Result = new Properties();
Element aElement = (Element)document.getFirstChild();
NodeList aNodeList = aElement.getElementsByTagName(aTagName) ;
for (int i=0 ;i<aNodeList.getLength();i++){
Node aNode = aNodeList.item(i);
Result.setProperty(aNode.getNodeName() ,aNode.getNodeValue());
}
return Result;
}
public Vector getAllAttributesByNodeList(String nodename) throws ConfigureFileException{
Document doc = document;
Element root = doc.getDocumentElement();
NodeList list = root.getElementsByTagName(nodename);
//System.out.println("NodeList length = " + list.getLength());
Vector v = new Vector(list.getLength());
for (int i=0; i<list.getLength(); i++)
{
Node node = list.item(i);
//get child node attributes
NamedNodeMap childlist = node.getAttributes();
if(childlist != null){
Properties p = new Properties();
for(int j=0; j<childlist.getLength(); j++){
Node childnode = childlist.item(j);
String attName = childnode.getNodeName();
String attValue = childnode.getNodeValue();
p.setProperty(attName,attValue);
}
v.addElement(p);
}
}
return v;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -