飛艷小屋

          程序--人生--哲學(xué)___________________歡迎艷兒的加入

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            52 Posts :: 175 Stories :: 107 Comments :: 0 Trackbacks

          package jp.co.nec.ome.utility.xml;

          import java.io.*;
          import javax.xml.parsers.*;

          import org.w3c.dom.*;
          import org.xml.sax.*;

          //*****************************************************************************
          /**
          ?* XML文書分析クラスです。<BR>
          ?* XMLに記述されている定義を分析し、子要素や子要素の內(nèi)容の取扱を容易にする
          ?* ユーティリティクラスです。
          ?* <P>
          ?* @author? Hisaya Saito
          ?* @version $Revision:?? 5.2? $ $Date:?? 20 May 2005 10:24:55? $
          ?* @since?? OpenMeisterEnterprise/EF 3.2
          ?*/
          //*****************************************************************************
          public class OmXMLAnalyzer {

          ? //***************************************************************************
          ? //コンストラクタ
          ? //***************************************************************************

          ? //***************************************************************************
          ? /**
          ?? * privateのコンストラクタです。<BR>
          ?? * スタティックメソッドのみの利用とし、インスタンス化は不許可とします。
          ?? */
          ? //***************************************************************************
          ? private OmXMLAnalyzer() {
          ? }

          ? //***************************************************************************
          ? //publicメソッド
          ? //***************************************************************************

          ? //***************************************************************************
          ? /**
          ?? * XMLファイルパスの文字列からXML文書のルート要素を取得します。
          ?? * @param? argFilePath XMLファイルパス
          ?? * @return ルート要素
          ?? * @exception FileNotFoundException 何らかの理由でファイルを開くことができない場合
          ?? * @exception ParserConfigurationException 要求された構(gòu)成を満たすDocumentBuilderを生成できない場合
          ?? * @exception SAXException 構(gòu)文解析エラーが発生した場合
          ?? * @exception IOException 入出力エラーが発生した場合
          ?? */
          ? //***************************************************************************
          ? public static Element getRootElement(String argFilePath)
          ?????????????????????????????????????????? throws FileNotFoundException,
          ????????????????????????????????????????????????? ParserConfigurationException,
          ????????????????????????????????????????????????? SAXException,
          ????????????????????????????????????????????????? IOException {

          ??? Element rootElement = null;

          ??? //XML文書解釈のためのParserの作成
          ??? File defineFile = new File(argFilePath);
          ??? InputStream inputStream = new FileInputStream(defineFile);
          ??? try {
          ????? DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
          ????? DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
          ????? Document document = documentBuilder.parse(inputStream);

          ????? //XML文書のツリーの最上位の要素rootを取得します。
          ????? rootElement = document.getDocumentElement();
          ??? } finally {
          ????? inputStream.close();
          ??? }
          ??? return rootElement;
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 入力ストリームからXML文書のルート要素を取得します。
          ?? * @param? argInputStream
          ?? * @return ルート要素
          ?? * @exception FileNotFoundException 何らかの理由でファイルを開くことができない場合
          ?? * @exception ParserConfigurationException 要求された構(gòu)成を満たすDocumentBuilderを生成できない場合
          ?? * @exception SAXException 構(gòu)文解析エラーが発生した場合
          ?? * @exception IOException 入出力エラーが発生した場合
          ?? */
          ? //***************************************************************************
          ? public static Element getRootElement(InputStream argInputStream)
          ????? throws FileNotFoundException,
          ???????????? ParserConfigurationException,
          ???????????? SAXException,
          ???????????? IOException {

          ??? Element rootElement = null;

          ??? //XML文書解釈のためのParserの作成
          ??? try {
          ????? DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
          ????? DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
          ????? Document document = documentBuilder.parse(argInputStream);

          ????? //XML文書のツリーの最上位の要素rootを取得します。
          ????? rootElement = document.getDocumentElement();
          ??? } finally {
          ????? argInputStream.close();
          ??? }
          ??? return rootElement;
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した親要素の中で最初の子要素を取得します。
          ?? * @param? argParentElement??? 親要素
          ?? * @param? argChildElementName 子要素名
          ?? * @return 最初に取得できた子要素
          ?? */
          ? //***************************************************************************
          ? public static Element getChildElement(Element argParentElement,
          ??????????????????????????????????????? String argChildElementName) {
          ??? return getChildElement(argParentElement, argChildElementName, 0);
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した親要素の中の指定した順番の子要素を取得します。
          ?? * @param? argParentElement?????? 親要素
          ?? * @param? argChildElementName???????? 子要素名
          ?? * @param? argSpecificationNumber 順番
          ?? * @return 指定した順番の子要素
          ?? */
          ? //***************************************************************************
          ? public static Element getChildElement(Element argParentElement,
          ??????????????????????????????????????? String argChildElementName,
          ??????????????????????????????????????? int argSpecificationNumber) {
          ??? NodeList childNodeList =
          ??????????????????? argParentElement.getElementsByTagName(argChildElementName);
          ??? return (Element)childNodeList.item(argSpecificationNumber);
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した親要素の中の全ての子要素を配列で取得します。
          ?? * @param? argParentElement?????? 親要素
          ?? * @param? argChildElementName??? 子要素名
          ?? * @return 子要素の配列
          ?? */
          ? //***************************************************************************
          ? public static Element[] getChildElementList(Element argParentElement,
          ????????????????????????????????????????????? String argChildElementName) {
          ??? NodeList childNodeList =
          ??????????????????? argParentElement.getElementsByTagName(argChildElementName);
          ??? int listCount = childNodeList.getLength();
          ??? if(listCount == 0) {
          ????? return null;
          ??? }
          ??? Element[] childElementList = new Element[listCount];
          ??? for (int i0 = 0; i0 < listCount; i0++) {
          ????? childElementList[i0] = (Element)childNodeList.item(i0);
          ??? }
          ??? return childElementList;
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した要素の中で指定する屬性の値が設(shè)定されている子要素を取得します。
          ?? * @param? argParentElement??? 親要素
          ?? * @param? argChildElementName 子要素名
          ?? * @param? argAttributeName??? 屬性名
          ?? * @param? argAttributeValue?? 屬性の値
          ?? * @return 指定する屬性の値が設(shè)定されている子要素で最初に取得できたもの
          ?? */
          ? //***************************************************************************
          ? public static Element getChildElement(Element argParentElement,
          ??????????????????????????????????????? String argChildElementName,
          ??????????????????????????????????????? String argAttributeName,
          ??????????????????????????????????????? String argAttributeValue) {

          ??? NodeList childNodeList = argParentElement.getElementsByTagName(argChildElementName);
          ??? int listLength = childNodeList.getLength();
          ??? if(listLength == 0) {
          ????? return null;
          ??? }
          ??? Element childElement = null;
          ??? for (int i0 = 0; i0 < listLength; i0++) {
          ????? Element localChildElement = (Element)childNodeList.item(i0);
          ????? String attributeValue = localChildElement.getAttribute(argAttributeName);
          ????? if (attributeValue.equals(argAttributeValue)) {
          ??????? childElement = localChildElement;
          ??????? break;
          ????? }
          ??? }

          ??? return childElement;
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した要素の內(nèi)容を取得します。
          ?? * @param? argElement 內(nèi)容を取得したい要素
          ?? * @return 內(nèi)容
          ?? */
          ? //***************************************************************************
          ? public static String getElementContents(Element argElement) {
          ??? if(argElement != null) {
          ????? Text text = (Text)argElement.getFirstChild();
          ????? if(text == null) {
          ??????? return null;
          ????? }
          ????? return text.getNodeValue();
          ??? } else {
          ????? return null;
          ??? }
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した親要素の中で最初の子要素の內(nèi)容を取得します。
          ?? * @param? argParentElement 親要素
          ?? * @param? argChildElementName? 內(nèi)容を取得したい子要素名
          ?? * @return 內(nèi)容
          ?? */
          ? //***************************************************************************
          ? public static String getChildElementContents(Element argParentElement,
          ?????????????????????????????????????????????? String argChildElementName) {

          ??? Element childElement = getChildElement(argParentElement,
          ?????????????????????????????????????????? argChildElementName);
          ??? return getElementContents(childElement);
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した親要素の中で指定する屬性の値が設(shè)定されている子要素の內(nèi)容を取得します。
          ?? * @param? argParentElement??? 親要素
          ?? * @param? argChildElementName 內(nèi)容を取得したい子要素名
          ?? * @param? argAttributeName??? 屬性名
          ?? * @param? argAttributeValue?? 屬性の値
          ?? * @return 內(nèi)容
          ?? */
          ? //***************************************************************************
          ? public static String getChildElementContents(Element argParentElement,
          ?????????????????????????????????????????????? String argChildElementName,
          ?????????????????????????????????????????????? String argAttributeName,
          ?????????????????????????????????????????????? String argAttributeValue) {

          ??? Element childElement = getChildElement(argParentElement,
          ?????????????????????????????????????????? argChildElementName,
          ?????????????????????????????????????????? argAttributeName,
          ?????????????????????????????????????????? argAttributeValue);
          ??? return getElementContents(childElement);
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した親要素の中で最初の子要素の屬性の値を取得します。
          ?? * @param? argParentElement??? 親要素
          ?? * @param? argChildElementName 子要素名
          ?? * @param? argAttributeName??? 屬性名
          ?? * @return 屬性の値
          ?? */
          ? //***************************************************************************
          ? public static String getAttributeValue(Element argParentElement,
          ???????????????????????????????????????? String argChildElementName,
          ???????????????????????????????????????? String argAttributeName) {
          ??? return getAttributeValue(argParentElement, argChildElementName, 0, argAttributeName);
          ? }

          ? //***************************************************************************
          ? /**
          ?? * 指定した親要素の中で指定した順番の子要素の屬性の値を取得します。
          ?? * @param? argParentElement?????? 親要素
          ?? * @param? argChildElementName??? 子要素名
          ?? * @param? argSpecificationNumber 順番
          ?? * @param? argAttributeName?????? 屬性名
          ?? * @return 屬性の値
          ?? */
          ? //***************************************************************************
          ? public static String getAttributeValue(Element argParentElement,
          ???????????????????????????????????????? String argChildElementName,
          ???????????????????????????????????????? int argSpecificationNumber,
          ???????????????????????????????????????? String argAttributeName) {
          ??? Element childElement = getChildElement(argParentElement,
          ?????????????????????????????????????????? argChildElementName,
          ?????????????????????????????????????????? argSpecificationNumber);
          ??? return childElement.getAttribute(argAttributeName);
          ? }
          }

          posted on 2006-03-20 15:57 天外飛仙 閱讀(415) 評論(0)  編輯  收藏 所屬分類: XML
          主站蜘蛛池模板: 乌鲁木齐市| 秭归县| 阜城县| 白玉县| 乌拉特前旗| 新蔡县| 曲麻莱县| 于都县| 灵宝市| 怀安县| 锦州市| 南康市| 沂南县| 富顺县| 佛坪县| 德钦县| 天水市| 大丰市| 望奎县| 昔阳县| 斗六市| 右玉县| 额敏县| 攀枝花市| 北辰区| 沾益县| 香格里拉县| 中卫市| 兴安县| 延寿县| 大城县| 同心县| 平阴县| 高雄市| 龙口市| 图片| 武城县| 岐山县| 门头沟区| 江山市| 吉安市|