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.aricojf.util.ConfigParser.java

/**
 * ??//  w  w w .j  a v  a 2  s  .co m
 * ?String "/?/?"
 * ep: "config/conn/user"
 * @return
 */
public static String parseConfigParmByPath(String configFilePath, String nodes) {
    value = "";
    FileInputStream fs = null;
    File file = new File(configFilePath);
    if (file.exists()) {
        file = null;
        try {
            fs = new FileInputStream(configFilePath);
            SAXReader reader = new SAXReader();
            org.dom4j.Document document = reader.read(fs);
            org.dom4j.Element element = document.getRootElement();
            String[] nod = nodes.trim().split(FILE_SEPARATOR);
            for (int i = 1; i < nod.length; i++) {
                List<Element> elements = element.elements(nod[i]);
                /**
                 * ???????
                 * ??xml?????
                 */
                for (Element ele : elements) {
                    element = ele;
                    value = ele.getText();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConn(fs);
        }
    }
    return null == value ? "" : value;
}

From source file:com.arthurh.xmergel.configuration.XmlCombinerConfig.java

License:Open Source License

@Bean
public SAXReader saxReader() {
    return new SAXReader();
}

From source file:com.augmentum.common.util.XMLFormatter.java

License:Open Source License

public static String toString(String xml, String indent) throws DocumentException, IOException {

    SAXReader reader = new SAXReader();

    Document doc = reader.read(new StringReader(xml));

    return toString(doc, indent);
}

From source file:com.bay12games.df.rawedit.xml.RawsDescriptionLoader.java

License:Open Source License

/**
 * Load and parse specified raws definition file. If elements is null, only
 * the content of the current file will be returned. If elements is not null
 * and contains not-null token and container maps, the content of current
 * file will be appended as if the definitions were in one file.<br><br>
 *
 * The instances of the underlying containers are preserved.<br><br>
 *
 * <p><strong>Note:</strong> There should be only one global access to the token/container
 * definitions in the application.<br>
 *
 * @param file The file to load and parse
 * @param elements The tuple-container for token, container and id elements
 * @return New instance of ElementContainer with maps of tokens and containers if the
 * supplied ElementContainer was null, or the same instance with updated content.
 * The supplied instance (possibly null) is returned on error
 *///  w ww. j a  v a2  s  .  c o m
public ElementContainer parse(File file, ElementContainer elements) {
    Document d;
    try {
        SAXReader reader = new SAXReader();
        d = reader.read(file);
    } catch (DocumentException ex) {
        log.error("Unable to load file:" + file + '.', ex);
        return elements;
    }

    if (elements == null) {
        containers = new HashMap<String, Container>();
        tokens = new HashMap<String, Token>();
        ids = new HashMap<String, Id>();
        elements = new ElementContainer(containers, tokens, ids);
    } else {
        if (elements.getContainers() != null) {
            containers = elements.getContainers();
        } else {
            containers = new HashMap<String, Container>();
        }

        if (elements.getTokens() != null) {
            tokens = elements.getTokens();
        } else {
            tokens = new HashMap<String, Token>();
        }

        if (elements.getIds() != null) {
            ids = elements.getIds();
        } else {
            ids = new HashMap<String, Id>();
        }
    }

    Element root = d.getRootElement();
    for (Element e : root.elements()) {
        if ("c".equals(e.getName())) {
            parseContainer(e);
        } else if ("t".equals(e.getName())) {
            parseToken(e);
        } else if ("id".equals(e.getName())) {
            parseId(e);
        }
    }

    return elements;//new ElementContainer(containers, tokens, ids);
}

From source file:com.beetle.framework.business.service.server.ServiceConfig.java

License:Apache License

private static void loadFromConfig(InputStream xmlFileInputStream) {
    SAXReader reader = new SAXReader();
    Document doc = null;//from  ww  w. j a  v  a2  s  .c  o m
    try {
        doc = reader.read(xmlFileInputStream);
        gendoc(doc);
    } catch (Exception de) {
        throw new AppRuntimeException(de);
    } finally {
        if (doc != null) {
            doc.clearContent();
            doc = null;
        }
        reader = null;
    }
}

From source file:com.beetle.framework.business.service.server.ServiceConfig.java

License:Apache License

private static void loadFromConfig(File f) {
    SAXReader reader = new SAXReader();
    Document doc = null;// w  ww. jav  a 2  s  .  c o m
    try {
        doc = reader.read(f);
        gendoc(doc);
    } catch (Exception de) {
        throw new AppRuntimeException(de);
    } finally {
        if (doc != null) {
            doc.clearContent();
            doc = null;
        }
        reader = null;
    }
}

From source file:com.beetle.framework.resource.dic.ReleBinder.java

License:Apache License

/**
 * ?/* w w w .ja  v a2 s .co  m*/
 * 
 * @param xmlFileInputStream
 *            --??
 */
public void bindFromConfig(InputStream xmlFileInputStream) {
    SAXReader reader = new SAXReader();
    Document doc = null;
    try {
        doc = reader.read(xmlFileInputStream);
        gendoc(doc);
        // bindProperties();
    } catch (Exception de) {
        throw new AppRuntimeException(de);
    } finally {
        if (doc != null) {
            doc.clearContent();
            doc = null;
        }
        reader = null;
    }
}

From source file:com.beetle.framework.resource.dic.ReleBinder.java

License:Apache License

/**
 * ? XML?<br>/*w  w w. j av  a  2s.c om*/
 * 
 * <pre>
 *  <binder>
 *     <!-- 
 * interface-?????
 * implement-??????interface
 * singleton-??implement?true
 *    -->
 *    <item interface=
"com.beetle.framework.util.pattern.di.IService" implement=
"com.beetle.framework.util.pattern.di.IServiceImp" singleton="true"/>
 * </binder>
 * </pre>
 * 
 * <br>
 * 
 * @param f
 *            --?
 */
public void bindFromConfig(File f) {
    SAXReader reader = new SAXReader();
    Document doc = null;
    try {
        doc = reader.read(f);
        gendoc(doc);
        // bindProperties();
    } catch (Exception de) {
        throw new AppRuntimeException(de);
    } finally {
        if (doc != null) {
            doc.clearContent();
            doc = null;
        }
        reader = null;
    }
}

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

License:Apache License

/**
 * ?xmldocument/*from   www .  ja va 2s .  c om*/
 * ??
 * @param filename
 * @return
 * @throws Exception
 */
public final static Document getXmlDoc(String filename) throws Exception {
    SAXReader reader = new SAXReader();
    File f = new File(filename);
    Document doc;
    if (f.exists()) {
        doc = reader.read(f);
    } else {
        InputStream is = ResourceLoader.getResAsStream(filename);
        doc = reader.read(is);
    }
    return doc;
}

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

License:Apache License

/**
 * //from   w w w .  jav  a 2 s . c om
 * @param xmlFileInputStream
 * @param itemPath
 * @param ElementName
 * @param keyName
 * @param valueName
 * @return
 */
public static Map<String, String> getProperties(InputStream xmlFileInputStream, String itemPath,
        String ElementName, String keyName, String valueName) {
    Map<String, String> map = new HashMap<String, String>();
    if (xmlFileInputStream == null) {
        // System.out.println("WARN:the resource do not exist");
        return map;
    }
    SAXReader reader = new SAXReader();
    Document doc = null;
    try {
        doc = reader.read(xmlFileInputStream);
        // 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();
                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!");
            // throw new RuntimeException("{" + itemPath +
            // "}does not exist");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (doc != null) {
            doc.clearContent();
        }
        reader = null;
    }
    // xmlFileInputStream.close();
    return map;
}