📄 xmlmanager.java
字号:
}
return nodes;
}
/**
* Add a node
*
*/
/*public void addNode(String nodeUri) throws ConfigureFileException
{
String parentUri,childUri;
int i=nodeUri.lastIndexOf(".");
parentUri=nodeUri.substring(0,i);
childUri=nodeUri.substring(i+1,nodeUri.length());
xmlParser.addNode(parentUri,childUri);
xmlParser.save();
}*/
public void addNode(String parentUri,String nodeName,int parentNo) throws ConfigureFileException
{
String nodeUri=parentUri;
int i=parentUri.lastIndexOf(".");
if (i==-1)
{
//root
xmlParser.addNode(parentUri,nodeName);
xmlParser.save();
}
else
{
parentUri=parentUri.substring(0,i);
// Node childNode=getNode(nodeUri,childNo);
// Node node=childNode.getParentNode();
Node node=getNode(parentUri,parentNo);
Element elem=document.createElement(nodeName);
node.appendChild(elem);
xmlParser.save();
}
}
public void addAttr(String parentUri,String currentNode,String attrName,String attrValue,int childNo,int parentNo)throws ConfigureFileException
{
int i=parentUri.lastIndexOf(".");
if(i==-1)
{
parentUri=parentUri+"."+currentNode;
xmlParser.addAttr(parentUri,attrName,attrValue);
xmlParser.save();
}
else
{
parentUri=parentUri.substring(0,i);
Node node=this.getNode(parentUri,parentNo);
Node child=this.getNode(node,childNo);
NamedNodeMap namedNodeMap = child.getAttributes();
Attr attr = document.createAttribute(attrName);
attr.setValue(attrValue);
namedNodeMap.setNamedItem(attr);
xmlParser.save();
}
}
/*public void addAttr(String nodeIdent,String attrName,String attrValue) throws ConfigureFileException
{
attrValue = new String(attrValue.getBytes("iso-8859-1"),"gb2312");
xmlParser.addAttr(nodeIdent,attrName,attrValue);
xmlParser.save();
}catch(Exception e){}
}
*/
//delete the node
public void deleteNode(String parentUri,String currentNode,int childNo,int parentNo)throws ConfigureFileException
{
String nodeUri=parentUri;
int i=parentUri.lastIndexOf(".");
if (i==-1)
{
//root
parentUri=parentUri+"."+currentNode;
xmlParser.removeNode(parentUri);
xmlParser.save();
}
else
{
parentUri=parentUri.substring(0,i);
Node node=this.getNode(parentUri,parentNo);
Node child=this.getNode(node,childNo);
if (child!=null)
node.removeChild(child);
xmlParser.save();
}
}
/*public void deleteNode(String nodeUri) throws ConfigureFileException
{
xmlParser.removeNode(nodeUri) ;
xmlParser.save();
}*/
//delete the attribute
public void deleteAttr(String parentUri,String currentNode,String attrName,int childNo,int parentNo) throws ConfigureFileException
{
String nodeUri=parentUri;
int i=parentUri.lastIndexOf(".");
if (i==-1)
{
//root
parentUri=parentUri+"."+currentNode+"."+attrName;
xmlParser.removeAttr(parentUri);
xmlParser.save();
}
else
{
parentUri=parentUri.substring(0,i);
System.out.println(parentUri);
Node node=this.getNode(parentUri,parentNo);
System.out.println(node.getNodeName());
Node child=this.getNode(node,childNo);
NamedNodeMap namedNodeMap = child.getAttributes() ;
namedNodeMap.removeNamedItem(attrName) ;
xmlParser.save();
}
}
/*public void deleteAttr(String attUri) 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]) ;
xmlParser.removeAttr(attUri);
xmlParser.save();
}*/
public Vector addStrTree(Vector xmlTree,String parent,int level,String title,String value,String type)
{
String[] strNode=new String[5];
strNode[0]=parent;
strNode[1]=Integer.toString(level);
strNode[2]=title;
strNode[3]=value;
strNode[4]=type;
xmlTree.addElement(strNode);
return xmlTree;
}
public void getTree(Node n) throws ConfigureFileException
{
try {
int type=n.getNodeType();
switch(type){
case Node.ATTRIBUTE_NODE:
String strAttr;
if(n.getParentNode()!=null)
strAttr=n.getParentNode().getNodeName();
else
strAttr=n.getNodeName();
xmlBranch=this.addStrTree(xmlBranch,strAttr,root,n.getNodeName(),n.getNodeValue(),"attr");
break;
case Node.DOCUMENT_NODE:
xmlBranch=this.addStrTree(xmlBranch,"root",root,"XmlTree","root","root");
break;
case Node.ELEMENT_NODE:
xmlBranch=this.addStrTree(xmlBranch,n.getParentNode().getNodeName(),root,n.getNodeName(),n.getNodeValue(),"elem");
NamedNodeMap atts = n.getAttributes();
root++;
for (int i = 0; i < atts.getLength(); i++)
{
Node att = atts.item(i);
getTree(att);
}
root--;
break;
default:
break;
}
root++;
for (Node child = n.getFirstChild(); child != null;child = child.getNextSibling())
{
getTree(child);
}
root--;
}catch(Exception e){
throw new ConfigureFileException(e.toString());
}
}
public void getNodeUrl(Node n) throws ConfigureFileException
{
int i;
String str1="";
String str2="";
try
{
int type=n.getNodeType();
if (type ==Node.ELEMENT_NODE)
{
if (n.getParentNode()!=null)
{
for(Node parent=n.getParentNode();parent!=null;parent=parent.getParentNode())
{
if (parent.getNodeName().equalsIgnoreCase("#document"))
break;
str1=parent.getNodeName()+"."+str1;
}
str1=str1+n.getNodeName();
}
else{
str1=n.getNodeName();
}
NamedNodeMap atts = n.getAttributes();
for (int j = 0; j < atts.getLength(); j++)
{
Node att = atts.item(j);
if (str2!="")
str2=str2+"."+att.getNodeValue().trim();
else
str2=att.getNodeValue().trim();
}
Hashtable htb=new Hashtable();
htb.put("Url",str1);
if (str2==""){
str2="NoValue";
}
htb.put("Val",str2);
allNodes.addElement(htb);
}
for (Node child = n.getFirstChild(); child!= null;child = child.getNextSibling()){
getNodeUrl(child);
}
}
catch(Exception e) {
throw new ConfigureFileException(e.toString());
}
}
public Vector getXmlConfigAtt() throws ConfigureFileException
{
Vector result=new Vector();
Vector temp=this.getAllNodes();
for(int i=0;i<temp.size();i++)
{
Hashtable htb=(Hashtable)temp.elementAt(i);
String[] str=this.getAllAttValue(htb.get("Url").toString());
if (str.length>0)
result.addElement(str);
}
return result;
}
/**
*判断当前节点是否已经存在
*/
public boolean isExistNode(String nodeUri) throws ConfigureFileException
{
Node tmp=xmlParser.getNode(nodeUri);
if (tmp==null)
return false;
else
return true;
}
/**
*@param:parentUri:父亲节点地路径
*@param:childNo:该节点是父亲节点的第几个儿子
public Node getNode(String parentUri,int childNo) throws ConfigureFileException
{
Node parent=xmlParser.getNode(parentUri);
NodeList childList=parent.getChildNodes();
System.out.println(childList.getLength());
// for (int i=0;i<childList.getLength();i++)
// System.out.println(childList.item(i).getNodeName());
return childList.item(childNo-1);
}
*/
public Node getNode(String parentUri,int childNo) throws ConfigureFileException
{
Node[] nodes=this.getChildNodes(parentUri);
return nodes[childNo];
}
public Node getNode(Node node,int childNo) throws ConfigureFileException
{
Node[] nodes=this.getChildNodes(node);
return nodes[childNo];
}
public static void main(String[] args) throws Exception
{
/*String file="D:\\BroadMAN\\conf\\CopyRightInfo.xml";
XmlManager xmlman=new XmlManager(file);
XmlParser xml=new XmlParser(file);
//xmlman.modifyNodeValue("AAA.AAAMap.Mapping","attribute","wilsonk",1);
//xmlman.addNode("CopyRight.Author","wilson",1);
//xmlman.addAttr("CopyRight.Author","value","wilson",1);
xmlman.deleteAttr("CopyRight.Author","desc",1,7);
// xml.save();
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -