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

📄 xmlmanager.java

📁 陕西电信sp客户端
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// ----------------------------------------------------------------------------
// $Source: /cvs/vas2006/webpro2/webpro_java/src/com/onewaveinc/portalman/webpro/configure/XmlManager.java,v $
// ----------------------------------------------------------------------------
// Copyright (c) 2002 by Onewave Inc.
// ----------------------------------------------------------------------------
// $Id: XmlManager.java,v 1.1.1.1 2006/08/01 05:49:33 zhengx Exp $
// ----------------------------------------------------------------------------
// $Log: XmlManager.java,v $
// Revision 1.1.1.1  2006/08/01 05:49:33  zhengx
// no message
//
// Revision 1.1  2006/06/02 03:33:15  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.1  2002/08/09 08:49:34  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.util.*;
import java.io.*;
import java.net.*;
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;




public class XmlManager
{

 private XmlParser xmlParser;
 private String filename;
 protected Document document;
 private  Vector allNodes = new Vector();
 private Vector xmlBranch=new Vector();
 private int root;

 public XmlManager(String fileName) throws ConfigureFileException
 {

       filename=fileName;
       xmlParser = new XmlParser(filename);
       document=xmlParser.getDocument();
       root=1;
 }

public Document createDoc(File file) throws ConfigureFileException
{
    Document aDoc=null;
    try{
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder docBuilder=factory.newDocumentBuilder();
       aDoc=docBuilder.parse(file);

    }
    catch(SAXException ee) {
	throw new ConfigureFileException("XML_FILE_PARSER_ERROR");
    }
    catch(IOException eee){
	throw new ConfigureFileException("XML_FILE_IO_ERROR");
    }
    catch(Exception e){
	throw new ConfigureFileException("XML_FILE_NOT_FOUND");
    }
    return aDoc;
}


public Document createDoc(InputStream inputStream) throws ConfigureFileException
{
  Document aDoc=null;
  try{
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder docBuilder=factory.newDocumentBuilder();
       aDoc=docBuilder.parse(inputStream);

      }
       catch(SAXException ee)
        {
            throw new ConfigureFileException("XML_FILE_PARSER_ERROR");
        }
        catch(IOException eee)
        {
            throw new ConfigureFileException("XML_FILE_IO_ERROR");
        }
        catch(Exception e)
        {
            throw new ConfigureFileException("XML_FILE_NOT_FOUND");
        }
    return aDoc;
}

 public Document createDoc(URL url) throws ConfigureFileException
 {
   Document aDoc=null;
   try{
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder docBuilder=factory.newDocumentBuilder();
       URLConnection xmlUrl=url.openConnection();
       xmlUrl.connect();
       aDoc=docBuilder.parse(xmlUrl.getInputStream());
      }
       catch(SAXException ee)
        {
            throw new ConfigureFileException("XML_FILE_PARSER_ERROR");
        }
        catch(IOException eee)
        {
            throw new ConfigureFileException("XML_FILE_IO_ERROR");
        }
        catch(Exception e)
        {
            throw new ConfigureFileException("XML_FILE_NOT_FOUND");
        }
    return aDoc;
}
 //create Document from fileName,and set the Document the propetry.
 public Document createDoc(String sFileName) throws ConfigureFileException
 {
   Document aDoc=null;
   try{
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder=factory.newDocumentBuilder();
         File f=new File(sFileName);
         aDoc=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 aDoc;
 }

 //return the branch of the tree
 public Vector getTreeBranch() throws ConfigureFileException
 {
  this.getTree(document);
  return xmlBranch;
 }

//save the node value
 public void saveDoc() throws  ConfigureFileException
    {
      xmlParser.save();
     /* try{
        FileWriter fileWriter=new FileWriter(filename);
        OutputFormat    format  = new OutputFormat(document,"GB2312",true);
        XMLSerializer    serial = new XMLSerializer( fileWriter, format );
        serial.asDOMSerializer();
        serial.serialize(document.getDocumentElement());
        }catch(Exception e)
        {
           throw new ConfigureFileException(e.toString());
        }*/

    }


//return the all node's url
 public Vector getAllNodes() throws ConfigureFileException
  {
     this.getNodeUrl(document);
     return allNodes;
   }


/*modify the node value
public void modifyNodeValue(String attrUri,String newAttrValue) throws ConfigureFileException
  {
    try
    {
     newAttrValue = new String(newAttrValue.getBytes("iso-8859-1"),"gb2312");
     xmlParser.modifyAttr(attrUri,newAttrValue);
     xmlParser.save();
    }catch(Exception e)
    {
    }
  }
*/
public void modifyNodeValue(String parentUri,String attrName,String newAttrValue,int childNo)
            throws ConfigureFileException
{
   try{
       newAttrValue = new String(newAttrValue.getBytes("iso-8859-1"),"gb2312");
       Node node=this.getNode(parentUri,childNo);
       NamedNodeMap namedNodeMap = node.getAttributes() ;
       System.out.println(attrName);
       Node myNode = namedNodeMap.getNamedItem(attrName) ;
       myNode.setNodeValue(newAttrValue) ;
       xmlParser.save();
  }catch(Exception e) {
        e.printStackTrace();
  }
}
//retrun a att value by the attName
public String getAttValue(String nodeUri,String attName) throws ConfigureFileException
{
   Node n=xmlParser.getNode(nodeUri);
   String str="NoValue";
   str=xmlParser.getAttributeValue(nodeUri+"."+attName);
   return str;
}



//return all att value of the node
public String[] getAllAttValue(String nodeUri) throws ConfigureFileException
{
   Node n=xmlParser.getNode(nodeUri);
   String[] str=new String[0];

   int type=n.getNodeType();
   if (type==Node.ELEMENT_NODE)
     {
       NamedNodeMap atts = n.getAttributes();
       if (atts!=null)
          {
           for (int i = 0; i < atts.getLength(); i++)
              {
                Node att = atts.item(i);
                String[] oldstr=str;
                str=new String[str.length+1];
                for(int j=0;j<oldstr.length;j++)
                  str[j]=oldstr[j];
                str[str.length-1]=xmlParser.getAttributeValue(nodeUri+"."+att.getNodeName());
              }
          }
      else
        str[0]=n.getNodeValue();

     }
  return str;
}

//return all child and the node's type is  element;
public String[] getAllChild(String nodeUri) throws ConfigureFileException
{
  String[] str=new String[0];
  Node n=xmlParser.getNode(nodeUri);
  int type=n.getNodeType();
  if (type==Node.ELEMENT_NODE)
  {
   for (Node child = n.getFirstChild(); child != null;child = child.getNextSibling())
      {
          if (child.getNodeType()!=Node.ELEMENT_NODE)
             continue;
          String[] oldstr=str;
          str=new String[str.length+1];
          for(int j=0;j<oldstr.length;j++)
             str[j]=oldstr[j];
          str[str.length-1]=child.getNodeName();
      }
  }
  return str;
}

/**
 * return all child and the node's type is  element;
 */
public Node[] getChildNodes(String nodeUri) throws ConfigureFileException
{
   Node[] nodes=new Node[0];
   Node n=xmlParser.getNode(nodeUri);
   int type=n.getNodeType();
   if(type==Node.ELEMENT_NODE)
   {
     for(Node child=n.getFirstChild();child!=null;child=child.getNextSibling())
     {
       if (child.getNodeType()!=Node.ELEMENT_NODE)
           continue;
       Node[] old=nodes;
       nodes=new Node[nodes.length+1];
       for(int j=0;j<old.length;j++)
          nodes[j]=old[j];
      nodes[nodes.length-1]=child;
     }
   }
  return nodes;
}

public Node[] getChildNodes(Node node) throws ConfigureFileException
{
  Node[] nodes=new Node[0];
  int type=node.getNodeType();
   if(type==Node.ELEMENT_NODE)
   {
     for(Node child=node.getFirstChild();child!=null;child=child.getNextSibling())
     {
       if (child.getNodeType()!=Node.ELEMENT_NODE)
           continue;
       Node[] old=nodes;
       nodes=new Node[nodes.length+1];
       for(int j=0;j<old.length;j++)
          nodes[j]=old[j];
      nodes[nodes.length-1]=child;
     }

⌨️ 快捷键说明

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