Example usage for org.dom4j.io SAXReader SAXReader

List of usage examples for org.dom4j.io SAXReader SAXReader

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader SAXReader.

Prototype

public SAXReader() 

Source Link

Usage

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static Map<String, String> getProperties(String xmlFileName, String itemPath, String ElementName,
        String keyName, String valueName) {
    Map<String, String> map = new HashMap<String, String>();
    SAXReader reader = new SAXReader();
    Document doc = null;/*from  www .j a  v  a 2 s . co  m*/
    try {
        //
        // InputStream
        // in=ClassLoader.getSystemResourceAsStream(xmlFileName);
        // Document doc=reader.read(in);
        //
        doc = reader.read(new File(xmlFileName));
        Node node = doc.selectSingleNode(convertPath(itemPath));
        if (node != null) {
            Iterator<?> it = node.selectNodes(ElementName).iterator();
            while (it.hasNext()) {
                Element e = (Element) it.next();
                map.put(e.valueOf("@" + keyName), e.valueOf("@" + valueName));
            }
        } else {
            // throw new com.beetle.framework.AppRuntimeException(
            // "?????!");
            System.out.println("WARN:Can't find the paht[" + itemPath + "],please check it!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (doc != null) {
            doc.clearContent();
        }
        reader = null;
    }
    return map;
}

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static String getTagContent(String xmlFileName, String TagPath) {
    String a = "";
    SAXReader reader = new SAXReader();
    Document doc = null;//from  www. ja v  a  2s.co m
    try {
        doc = reader.read(new File(xmlFileName));
        Node node = doc.selectSingleNode(convertPath(TagPath));
        if (node != null) {
            a = node.getText();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (doc != null) {
            doc.clearContent();
        }
        reader = null;
    }
    return a;
}

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

/**
 * ???????//from   ww w.  j  av  a 2s.c om
 * 
 * 
 * @param xmlFileInputStream
 * @param TagPath
 * @return
 */
public static String getTagContent(InputStream xmlFileInputStream, String TagPath) {
    String a = "";
    if (xmlFileInputStream == null) {
        // System.out.println("WARN:the resource do not exist");
        return a;
    }
    SAXReader reader = new SAXReader();
    Document doc = null;
    try {
        doc = reader.read(xmlFileInputStream);
        Node node = doc.selectSingleNode(convertPath(TagPath));
        if (node != null) {
            a = node.getText();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (doc != null) {
            doc.clearContent();
        }
        reader = null;
    }
    return a;
}

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static void setTagContent(String xmlFileName, String TagPath, String value) {
    synchronized (writeLock) {
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;/*  www.j a va2 s  . c om*/
        try {
            Document doc = reader.read(new File(xmlFileName));
            Node node = doc.selectSingleNode(convertPath(TagPath));
            if (node != null) {
                node.setText(value);
            }
            writer = new XMLWriter(new FileWriter(xmlFileName));
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            reader = null;
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static void setProperties(String xmlFileName, String itemPath, String ElementName, String keyName,
        String valueName, String key, String value) {
    // ? ??????//  www . j ava  2  s .com
    // ????
    synchronized (writeLock) {
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        try {
            Document doc = reader.read(new File(xmlFileName));
            Node node = doc.selectSingleNode(convertPath(itemPath));
            if (node != null) {
                Iterator<?> it = node.selectNodes(ElementName).iterator();
                while (it.hasNext()) {
                    Element e = (Element) it.next();
                    if (e.attributeValue(keyName).equals(key)) {
                        e.addAttribute(valueName, value);
                        break;
                    }
                }
            }
            writer = new XMLWriter(new FileWriter(xmlFileName));
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            reader = null;
        }
    }
}

From source file:com.beetle.framework.web.cache.imp.CacheConfig.java

License:Apache License

public synchronized void readCacheURLs(InputStream xmlIs) {
    url_cacheAttr.clear();/*  w  ww. j av a2 s.c  om*/
    if (xmlIs == null) {
        return;
    }
    SAXReader reader = new SAXReader();
    Document doc = null;
    try {
        doc = reader.read(xmlIs);
        Node node = doc.selectSingleNode(convertPath("mappings.caches"));
        if (node != null) {
            config.setDiskStorePath(node.valueOf("@diskStorePath"));
            config.setMaxElementsInMemory(toInt(node.valueOf("@maxElementsInMemory")));
            config.setMemoryStoreEvictionPolicy(node.valueOf("memoryStoreEvictionPolicy"));
            Iterator<?> it = node.selectNodes("cItem").iterator();
            while (it.hasNext()) {
                CacheAttr attr = new CacheAttr();
                Element e = (Element) it.next();
                attr.setUrl(e.valueOf("@name"));
                attr.setScope(e.valueOf("@scope"));
                attr.setTime(toInt(e.valueOf("@time")));
                url_cacheAttr.put(attr.getUrl(), attr);
            }
        }
    } catch (Exception de) {
        de.printStackTrace();
    } finally {
        if (doc != null) {
            doc.clearContent();
        }
        reader = null;
    }
}

From source file:com.beyondb.io.DBConfig.java

/**
 *??/*from   w  w  w . j  ava 2s. c o m*/
 * @param path
 * @return
 */
public static DataSource readDBConfig(String path) {
    DataSource ds = null;

    try {

        SAXReader reader = new SAXReader();
        File file = new File(path);

        if (file.exists()) {
            Document doc = reader.read(file);

            Element datasource = doc.getRootElement();
            Element dbElement = datasource.element(Node_database);
            if (dbElement != null) {
                String id = dbElement.attribute(Attribute_id).getValue();
                String username = dbElement.attribute(Attribute_username).getValue();
                String password = dbElement.attribute(Attribute_password).getValue();
                String virtualNode = dbElement.attribute(Attribute_virtualNode).getValue();
                String url = dbElement.elementText(Attribute_jdbcurl);
                ds = new BydDataSource(url, username, password);
                ds.setVirtualNode(virtualNode);
                ds.setID(id);
            }

        }

    } catch (DocumentException ex) {

        Logger.getLogger(DBConfig.class.getName()).log(Level.SEVERE, "???", ex);
    }
    return ds;
}

From source file:com.beyondb.io.DBConfig.java

/**
 *??/*from w  w w. j  ava 2  s .  c  o m*/
 * @param path
 * @return
 */
public static ArrayList<DataSource> readDBConfig1(String path) {
    ArrayList<DataSource> dslist = new ArrayList<>();

    try {

        SAXReader reader = new SAXReader();
        File file = new File(path);

        if (file.exists()) {
            Document doc = reader.read(file);

            Element datasource = doc.getRootElement();
            Iterator it = datasource.elementIterator(Node_database);
            while (it.hasNext()) {
                Element dbElement = (Element) it.next();
                String id = dbElement.attribute(Attribute_id).getValue();
                String username = dbElement.attribute(Attribute_username).getValue();
                String password = dbElement.attribute(Attribute_password).getValue();
                String virtualNode = dbElement.attribute(Attribute_virtualNode).getValue();
                String url = dbElement.elementText(Attribute_jdbcurl);

                DataSource ds = new BydDataSource(url, username, password);
                ds.setVirtualNode(virtualNode);
                ds.setID(id);
                dslist.add(ds);
            }

        }

    } catch (DocumentException ex) {

        Logger.getLogger(DBConfig.class.getName()).log(Level.SEVERE, "???", ex);
    }
    return dslist;
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * ReaderDocument encodingStrnull""GB2312
 *
 * @param in// w  w w. j av a  2 s  .  c  om
 *            Reader
 * @param encoding
 *            
 * @return documment
 * @throws XMLDocException
 * @throws BaseException
 */
public static Document fromXML(Reader in, String encoding) throws BaseException {
    try {
        if (encoding == null || encoding.equals("")) {
            encoding = DEFAULT_ENCODING;
        }
        SAXReader reader = new SAXReader();
        InputSource source = new InputSource(in);
        source.setEncoding(encoding);
        Document document = reader.read(source);
        document.setXMLEncoding(encoding);
        return document;
    } catch (Exception ex) {
        throw new BaseException("UTIL-0001", ex);
    }

}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XMLDocument encodingStrnull""GB2312//from w w  w .  j av  a2 s.co m
 *
 * @param inputSource
 *            
 * @param encoding
 *            
 * @return document
 * @throws XMLDocException
 * @throws BaseException
 */
public static Document fromXML(InputStream inputSource, String encoding) throws BaseException {
    try {
        if (encoding == null || encoding.equals("")) {
            encoding = DEFAULT_ENCODING;
        }
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputSource);
        document.setXMLEncoding(encoding);
        return document;
    } catch (Exception ex) {
        throw new BaseException("UTIL-0001", ex);
    }
}