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

📄 xmlutil.java

📁 Rakiura JFern是一个非常轻型的带有模拟器的Petri网络框架
💻 JAVA
字号:
// Copyright (C) 1999-2002 by Mariusz Nowostawski and others  [http://www.rakiura.org]// This file is part of nzdis-util package. See the file LICENSE for copyright information // and the terms and conditions for copying, distribution and modification of nzdis-util package.package org.rakiura.cpn;/**/import java.io.*;import java.util.*;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Text;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.ParserConfigurationException;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;/** * Set of utilities for XML processing.  * This is a class supporting the use of Sun Java XML API (jaxp) and * is currently using Project X library for XML parsing.  *  * Created: Mon Jun 28 14:51:25 1999 * *@author  <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version 2.1.0 $Revision: 1.2 $ *@since 1.0 */public class XmlUtil {  /**    * Normalizes the given string. */  public static String normalize(String s) {    StringBuffer str = new StringBuffer();    int len = (s != null) ? s.length() : 0;    for (int i = 0; i < len; i++) {      char ch = s.charAt(i);      switch (ch) {      case '<': {        str.append("&lt;");        break;      }      case '>': {        str.append("&gt;");        break;      }      case '&': {        str.append("&amp;");        break;      }      case '"': {        str.append("&quot;");        break;      }      /*        case '\r':        case '\n': {        str.append("&#");        str.append(Integer.toString(ch));        str.append(';');        break;        }       **/      default: {        str.append(ch);      }      }    }    return str.toString();  }  /**/  public static Document parseFile(String filename)    throws SAXException, ParserConfigurationException {    Properties props;    props = new Properties ();    return parseFile(filename, props);  }  /**/  public static Document parseFile(String filename, String propfilename)    throws SAXException, ParserConfigurationException {    Properties props;    props = new Properties();    try{      InputStream propsStream = new FileInputStream(propfilename);      props.load (propsStream);    }catch(FileNotFoundException ex){ex.printStackTrace();    }catch(IOException ex1){ex1.printStackTrace();}    return parseFile(filename, props);  }  /**/  public static Document parseFile(String filename, Properties props)     throws SAXException, ParserConfigurationException {    InputStream input = null;    try{      input = new FileInputStream(filename);    }catch(FileNotFoundException ex){ex.printStackTrace(); return null;}    return parse(input, props);  }  /**/  public static Document parse(InputStream input, Properties props)     throws SAXException, ParserConfigurationException {    return parse(new InputSource(input), props);  }  /**/  public static Document parse(String input)     throws SAXException, ParserConfigurationException {    return parse(new StringReader(input), null);  }  /**/  public static Document parse(Reader input)     throws SAXException, ParserConfigurationException {    return parse(new InputSource(input), null);  }      /**/  public static Document parse(Reader input, Properties props)     throws SAXException, ParserConfigurationException {    return parse(new InputSource(input), props);  }  /**/  public static Document parse(InputSource input, Properties props)    throws SAXException, ParserConfigurationException {      Document doc=null;      try{          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();          DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();          doc = docBuilder.parse(input);    } catch (IOException e) {      e.printStackTrace(System.out);      //throw e;    }          return doc;  }  /**   * Creates ElementNode for given tag with particular text as content.   */  public static Element createTextNode(String tag, String content)      throws ParserConfigurationException{    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();    Document doc = docBuilder.newDocument();    Element node = doc.createElement(tag);    Text t = doc.createTextNode(content);    node.appendChild(t);    return node;  }} // XmlUtil//////////////////// end of file ////////////////////

⌨️ 快捷键说明

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