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

📄 xmlobject.java

📁 数字图书馆的互操作接口
💻 JAVA
字号:
package dli2fe.xml;

/**
 * Title:        Digial Library Interoperable Interface Fudan Edition
 * Description:  This project contains all the classes required for DLI2FE interface. Developers use these classes to implement the wrapper and client side codes. The basic functions of DLI2FE is as follows:
 * Search: Search a digital library source site.
 * Metadata: Fetch metadata for a site.
 * ResultAccess: Get results for a given query.
 * DLI2FE uses Dublin Core as the basic attribute model, DAV/DASL as the general XML-based query language and CORBA as distributed object transportation mechanism.
 * Copyright:    Copyright (c) 2001
 * Company:      Fudan University
 * @author Carl Tao
 * @version 1.0
 */
import org.w3c.dom.*;
import java.io.*;
import dli2fe.DLI2FEException;
import com.sun.xml.tree.XmlWriteContext;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.tools.codec.*;
import dli2fe.DLI2FE;
import org.xml.sax.SAXException;

public class XMLObject implements dli2fe.XMLObject {
  static DocumentBuilderFactory dbf;
  static DocumentBuilder db;
  static Document document;
  Element root;
  String str;

  public XMLObject() {
    str = null;
    if (db == null) {
      // Init the document builder
      dbf = DocumentBuilderFactory.newInstance();

      dbf.setValidating(false);
      dbf.setIgnoringComments(true);
      dbf.setIgnoringElementContentWhitespace(true);
      dbf.setCoalescing(false);
      // The opposite of creating entity ref nodes is expanding them inline
      dbf.setExpandEntityReferences(true);
      dbf.setNamespaceAware(true);
      try {
        db = dbf.newDocumentBuilder();
        document = db.newDocument();
      } catch (ParserConfigurationException pce) {
        System.out.println(pce);
        System.exit(1);
      }
    }
  }

  public XMLObject(Element el) {
    this();
    setElement(el);
  }

  public XMLObject(String str) throws DLI2FEException {
    this();
    setString(str);
  }

  public XMLObject(File file) throws DLI2FEException {
    this();
    try {
      Document doc = db.parse(file);
      setElement(doc.getDocumentElement());
    } catch (SAXException se) {
      throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Malformed XML file: " + file.getPath());
    } catch (IOException ioe) {}
  }

  public Element getElement() {
    return root;
  }

  public void setElement(Element el) {
    root = (Element)document.importNode(el, true);
  }

  public String getString() {
/*    StringWriter sw = new StringWriter();
    XmlWriteContext xwc = new XmlWriteContext(sw);
    try {
      ((ElementNode)root).writeXml(xwc);
      return sw.toString();
    } catch (IOException ioe) {
      return "";
    }
*/
    return root.toString();
  }

  public void setString(String XMLStr) throws DLI2FEException {
    str = XMLStr;
    if (XMLStr != null && XMLStr.length() > 0)
      setInputSource(new InputSource(new StringReader(XMLStr)));
  }

  public void setInputSource(InputSource is) throws DLI2FEException {
    try {
      Document doc = db.parse(is);
      setElement(doc.getDocumentElement());
      str = null;
    } catch (SAXException se) {
      throw new DLI2FEException(DLI2FEException.MALFORMED_XML_EXC, "Malformed XMLObject string: " + str);
    } catch (IOException ioe) {}
  }

  public static XMLObject create(dli2fe.XMLObject obj) throws DLI2FEException {
    if(obj == null)
      return new XMLObject();
    if(obj instanceof XMLObject)
      return (XMLObject)obj;
    else
      return new XMLObject(obj.getString());
  }

  public static void setElement(dli2fe.XMLObject obj, Element el) throws DLI2FEException {

    if(obj instanceof XMLObject)
      ((XMLObject)obj).setElement(el);
    else {
      XMLObject xmlobj = new XMLObject(el);
      obj.setString( xmlobj.getString() );
    }
  }

  public static Document getDocument() {
    return document;
  }

  public static Document createDocument() {
    XMLObject obj = new XMLObject();
    return db.newDocument();
  }

  public static void main(String[] args) {
    try {
      XMLObject x1 =new XMLObject("<c xmlns='http://tellmeplease/#' xmlns:a='http://helloworld/#'><like><a:name>student</a:name><value>hello</value></like></c>");//"<?xml version=\"1.0\" encoding=\"GB2312\" ?><basicsearch xmlns=\"http://www.cs.fudan.edu.cn/DLI2FE/1.0#\" xmlns:dc=\"http://purl.org/metadata/dublin_core#\" xmlns:mm=\"http://www.cs.fudan.edu.cn/multimedia/#\"> <where>  <and>  <eq>     <prop><dc:Contributor><dc:Translator/></dc:Contributor></prop>    <literal>施伯乐</literal> </eq>  <like>  <prop><mm:image/></prop>   <literal>file://D:\\Temp\\bookinput.jpg</literal>   </like>  </and> </where> </basicsearch>");
      Element el = (Element)x1.getElement().getFirstChild();
      Element el1 = DOMUtil.getDescendant(el, "http://helloworld/#", "name");
      System.out.println(el1);
    } catch (Exception any) {
      any.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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