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.iih5.smartorm.kit.SqlXmlKit.java

License:Apache License

private void init(File dataDir) {
    try {/*  ww  w  .  ja v  a2 s .c om*/
        List<File> files = new ArrayList<File>();
        listDirectory(dataDir, files);
        for (File file : files) {
            if (file.getName().contains(".xml")) {
                SAXReader reader = new SAXReader();
                Document document = reader.read(file);
                Element xmlRoot = document.getRootElement();
                for (Object e : xmlRoot.elements("class")) {
                    Map<String, String> methods = new HashMap<String, String>();
                    Element clasz = (Element) e;
                    for (Object ebj : clasz.elements("sql")) {
                        Element sql = (Element) ebj;
                        methods.put(sql.attribute("method").getValue(), sql.getText());
                    }
                    resourcesMap.put(clasz.attributeValue("name"), methods);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.infinities.skyport.util.XMLUtil.java

License:Apache License

public static Document parse(File file)
        throws DocumentException, UnsupportedEncodingException, FileNotFoundException {
    final SAXReader reader = new SAXReader();
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    final Document document = reader.read(br);

    return document;
}

From source file:com.ischool.weixin.tool.MessageUtil.java

License:Open Source License

public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
    // ?HashMap/*from w  ww . ja v a 2  s .co  m*/
    Map<String, String> map = new HashMap<String, String>();

    // request??
    InputStream inputStream = request.getInputStream();
    // ??
    SAXReader reader = new SAXReader();
    Document document = reader.read(inputStream);
    // xml
    Element root = document.getRootElement();
    // ?
    @SuppressWarnings("unchecked")
    List<Element> elementList = root.elements();

    // ???
    for (Element e : elementList) {
        map.put(e.getName(), e.getText());
    }
    // ?
    inputStream.close();
    inputStream = null;

    return map;
}

From source file:com.iterzp.momo.utils.SettingUtils.java

License:Open Source License

/**
 * ?//w w  w  .j  av a 2 s. c  o  m
 * 
 * @return 
 */
public static Setting get() {
    Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
    net.sf.ehcache.Element cacheElement = cache.get(Setting.CACHE_KEY);
    Setting setting;
    if (cacheElement != null) {
        setting = (Setting) cacheElement.getObjectValue();
    } else {
        setting = new Setting();
        try {
            File momoXmlFile = new ClassPathResource(CommonAttributes.MOMO_XML_PATH).getFile();
            Document document = new SAXReader().read(momoXmlFile);
            List<Element> elements = document.selectNodes("/momo/setting");
            for (Element element : elements) {
                String name = element.attributeValue("name");
                String value = element.attributeValue("value");
                try {
                    beanUtils.setProperty(setting, name, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    }
    return setting;
}

From source file:com.iterzp.momo.utils.SettingUtils.java

License:Open Source License

/**
 * // w w  w  .j  ava2s  . c o m
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.MOMO_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/momo/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.itextpdf.rups.model.XfaFile.java

License:Open Source License

/**
 * Constructs an XFA file from an OutputStreamResource.
 * This resource can be an XML file or a node in a RUPS application.
 * @param   resource   the XFA resource//  ww  w  .j  a va  2s. c  o  m
 * @throws IOException 
 * @throws DocumentException 
 */
public XfaFile(OutputStreamResource resource) throws IOException, DocumentException {
    // Is there a way to avoid loading everything in memory?
    // Can we somehow get the XML from the PDF as an InputSource, Reader or InputStream?
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    resource.writeTo(baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    SAXReader reader = new SAXReader();
    xfaDocument = reader.read(bais);
}

From source file:com.itfsw.mybatis.generator.plugins.utils.enhanced.TemplateCommentGenerator.java

License:Apache License

/**
 * /*from  w ww  .j  a  va  2s. c o m*/
 * @param templatePath ?
 * @param useForDefault Comment??
 */
public TemplateCommentGenerator(String templatePath, boolean useForDefault) {
    try {
        Document doc = null;
        if (useForDefault) {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath);
            doc = new SAXReader().read(inputStream);
            inputStream.close();
        } else {
            File file = new File(templatePath);
            if (file.exists()) {
                doc = new SAXReader().read(file);
            } else {
                logger.error("?:" + templatePath);
            }
        }

        // ??comment 
        if (doc != null) {
            for (EnumNode node : EnumNode.values()) {
                Element element = doc.getRootElement().elementByID(node.value());
                if (element != null) {
                    Configuration cfg = new Configuration(Configuration.VERSION_2_3_26);
                    // ?
                    Template template = new Template(node.value(), element.getText(), cfg);
                    templates.put(node, template);
                }
            }
        }
    } catch (Exception e) {
        logger.error("?XML??", e);
    }
}

From source file:com.jaspersoft.jasperserver.export.ImporterImpl.java

License:Open Source License

protected Document readIndexDocument() {
    InputStream indexInput = getIndexInput();
    boolean close = true;
    try {//from  w  w  w.j av a 2s .  c o m
        SAXReader reader = new SAXReader();
        reader.setEncoding(getCharacterEncoding());
        Document document = reader.read(indexInput);

        close = false;
        indexInput.close();

        return document;
    } catch (IOException e) {
        log.error(e);
        throw new JSExceptionWrapper(e);
    } catch (DocumentException e) {
        log.error(e);
        throw new JSExceptionWrapper(e);
    } finally {
        if (close) {
            try {
                indexInput.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Builds the document XML model up based the given reader of XML data.
 *
 * @param in the input stream used to build the xml document
 * @throws IOException thrown when an error occurs reading the input stream.
 *//*from   www . jav a2s  .  com*/
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        document = xmlReader.read(in);
    } catch (Exception e) {
        Log.error("Error reading XML properties", e);
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.jga.view.seguridad.RolManagedBean.java

protected List<Rol> getSecurityRoles() {

    HttpServletRequest origRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();/*from   www  .  jav  a 2 s  .  c  o  m*/
    ServletContext sc = origRequest.getServletContext();
    InputStream is = sc.getResourceAsStream("/WEB-INF/web.xml");

    try {
        SAXReader reader = new SAXReader();
        Document doc = reader.read(is);

        Element webApp = doc.getRootElement();

        // Type safety warning:  dom4j doesn't use generics
        List<Element> roleElements = webApp.elements("security-role");
        for (Element roleEl : roleElements) {
            roles.add(new Rol(roleEl.element("role-name").getText()));
        }
    } catch (EJBException ex) {
        Exception ne = (Exception) ex.getCause();
        Logger.getLogger(UsuarioManagedBean.class.getName()).log(Level.SEVERE, null, ne);
    } catch (Exception ex) {
        Logger.getLogger(UsuarioManagedBean.class.getName()).log(Level.SEVERE, null, ex);
    }

    return roles;
}