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

📄 octopusxmlutil.java

📁 数据仓库工具
💻 JAVA
字号:
/**
  OctopusXMLUtil - Utils for reading attributes form xml tags.

    Copyright (C) 2002-2003  Together

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 OctopusXMLUtil.java
 Date: 20.5.2003.
 @version 1.0.0
 @author: Zoran Milakovic zoran@prozone.co.yu
 */

package org.webdocwf.util.loader;

import java.util.Vector;
import org.w3c.dom.*;

/**
 * Class has utility methods.
 *
 * @author     Zoran Milakovic
 * @version    1.1
 */
public class OctopusXMLUtil {


  /**
   * Gets attribute values from specified tags.
   * @param tags NodeList of tags to check for attribute
   * @param attrName name of attribute
   * @param defaultValue defaultValue if attribute is not defined
   * @return vector which contains values
   */
  public static Vector getAttributeValues (NodeList tags, String attrName, String defaultValue) {
    Vector vecValues = new Vector();
    String nodeValue = "";
    if (attrName != null) {
      for (int i = 0; i < tags.getLength(); i++) {
        NamedNodeMap attrs = tags.item(i).getAttributes();
        Node nodeResult = attrs.getNamedItem(attrName);
        if (nodeResult != null)
          nodeValue = nodeResult.getNodeValue();
        else
          nodeValue = defaultValue;
        vecValues.addElement(nodeValue);
      }
    }
    return  vecValues;
  }

  /**
   * Method importAttributeValue reads value for strAttrName attribute in strTagName tag.
   * This method return this value.
   * @param doc Parsed import XML file.
   * @param strTagName The name of tag where attribute is situated.
   * @param strAttrName The name of tag attribute which reads input value.
   * @param iImportJobItem Number of ImportDefinition tag which is processed.
   * @return String - importing value.
   */
  public static String importAttributeValue (Document doc, String strTagName, String strAttrName,
      int iImportJobItem) {
    String strValue = "";
    NodeList tagBasic = doc.getElementsByTagName(strTagName);
    Element docFragment = (Element)tagBasic.item(iImportJobItem);
    if (docFragment != null)
      strValue = docFragment.getAttribute(strAttrName);
    return  strValue;
  }

  /**
   * Method importAttribute reads value for strAttrName attribute in strTagName tag.
   * This method return this value.
   * @param doc Parsed import XML file.
   * @param strTagName The name of tag where attribute is situated.
   * @param strAttrName The name of tag attribute which reads input value.
   * @param iImportJobItem Number of ImportDefinition tag which is processed.
   * @return String - importing value.
   */
  public static String importAttribute(Document doc, String strTagName, String strAttrName,
                                       int iImportJobItem) {
    String strValue = "";
    NodeList tagBasic = doc.getElementsByTagName("importDefinition");
    if (tagBasic.getLength() != 0) {
      Element docFragment = (Element)tagBasic.item(iImportJobItem);
//      NodeList tag = docFragment.getElementsByTagName(tagName);
//      for (int i = 0; i < tag.getLength(); i++) {

      tagBasic = docFragment.getElementsByTagName(strTagName);
      if(tagBasic.getLength()!=0) {
        docFragment = (Element)tagBasic.item(0);
        if (docFragment != null)
          strValue = docFragment.getAttribute(strAttrName);
      }
    }
    return  strValue;
  }

  /**
   * Method importValue reads values from desired XML tag and puts them into Vector.
   * @param doc Parsed import XML file.
   * @param tagName The name of XML tag.
   * @param strAttrName The name of tag attribute which reads input strValue.
   * @param iImportJobItem Number of ImportDefinition tag which is processed.
   * @return Vector of importing values.
   */
  public static Vector importValue (Document doc, String tagName, String strAttrName,
                                    int iImportJobItem) {
    Vector strValue = new Vector();
    NodeList tagBasic = doc.getElementsByTagName("importDefinition");
    if (tagBasic.getLength() != 0) {
      Element docFragment = (Element)tagBasic.item(iImportJobItem);
      NodeList tag = docFragment.getElementsByTagName(tagName);
      for (int i = 0; i < tag.getLength(); i++) {
        String nodeValue = "";
        if (strAttrName != null) {
          NamedNodeMap attrs = tag.item(i).getAttributes();
          Node nodeResult = attrs.getNamedItem(strAttrName);
          if (nodeResult != null)
            nodeValue = nodeResult.getNodeValue();
          strValue.addElement(nodeValue);
        }
        else {
          NodeList nodeText = tag.item(i).getChildNodes();
          if (nodeText.item(0) != null) {
            nodeValue = nodeText.item(0).getNodeValue();
            strValue.addElement(nodeValue);
          }
        }
      }
    }
    return  strValue;
  }
  /**
	 * Method importValue reads values from desired XML tag and puts them into Vector.
	 * @param docFragment Parsed import XML file.
	 * @param tagName The name of XML tag.
	 * @param strAttrName The name of tag attribute which reads input strValue.
	 * @return Vector of importing values.
	 */
	public static Vector importValueForTransform (Element docFragment, String tagName, String strAttrName) {
		
	  Vector strValue = new Vector();
	  NodeList tag = docFragment.getElementsByTagName(tagName);
		for (int i = 0; i < tag.getLength(); i++) {
		  String nodeValue = "";
		  if (strAttrName != null){
			NamedNodeMap attrs = tag.item(i).getAttributes();
			Node nodeResult = attrs.getNamedItem(strAttrName);
			if (nodeResult != null)
			  nodeValue = nodeResult.getNodeValue();
			      strValue.addElement(nodeValue);
		  }
		  else {
			NodeList nodeText = tag.item(i).getChildNodes();
			if (nodeText.item(0) != null) {
			  nodeValue = nodeText.item(0).getNodeValue();
			  strValue.addElement(nodeValue);
			}
		  }
	  }
	  return  strValue;
	}
  /**
   * Method importValue reads values from desired XML tag and puts them into Vector.
   * @param doc Parsed import XML file.
   * @param tagName The name of XML tag.
   * @param strAttrName The name of tag attribute which reads input strValue.
   * @param iImportJobItem Number of ImportDefinition tag which is processed.
   * @param defaultValue The default value of strattrname attribute.
   * @return Vector of importing values.
   */
  public static Vector importValue (Document doc, String tagName, String strAttrName,
                                    int iImportJobItem, String defaultValue) {
    Vector strValue = new Vector();
    NodeList tagBasic = doc.getElementsByTagName("importDefinition");
    if (tagBasic.getLength() != 0) {
      Element docFragment = (Element)tagBasic.item(iImportJobItem);
      NodeList tag = docFragment.getElementsByTagName(tagName);
      for (int i = 0; i < tag.getLength(); i++) {
        String nodeValue = "";
        if (strAttrName != null) {
          NamedNodeMap attrs = tag.item(i).getAttributes();
          Node nodeResult = attrs.getNamedItem(strAttrName);
          if (nodeResult != null)
            nodeValue = nodeResult.getNodeValue();
          else
            nodeValue = defaultValue;
          strValue.addElement(nodeValue);
        }
        else {
          NodeList nodeText = tag.item(i).getChildNodes();
          if (nodeText.item(0) != null) {
            nodeValue = nodeText.item(0).getNodeValue();
            strValue.addElement(nodeValue);
          }
        }
      }
    }
    return  strValue;
  }
  

  /**
   * @return Vector of importing values.
   * 
   */
  //ZK change this 16.04.2004.Change Return type from Document to Element 
  public static Element getDocumentFragment (Document doc, String tagName, int iCurrent,
                                    int iImportJobItem) {

    NodeList tagBasic = doc.getElementsByTagName("importDefinition");
    if (tagBasic.getLength() != 0) {
      Element docFragment = (Element)tagBasic.item(iImportJobItem);
      NodeList tag = docFragment.getElementsByTagName(tagName);
      if(iCurrent < tag.getLength())
         return (Element)tag.item(iCurrent);
      else
         return null;
  }
  else
      return null;
}
  
}

⌨️ 快捷键说明

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