Example usage for org.dom4j Document getRootElement

List of usage examples for org.dom4j Document getRootElement

Introduction

In this page you can find the example usage for org.dom4j Document getRootElement.

Prototype

Element getRootElement();

Source Link

Document

Returns the root Element for this document.

Usage

From source file:com.apicloud.commons.model.Config.java

License:Open Source License

public static Config loadXml(InputStream input) {
    Config config = new Config();
    if (input == null) {
        return null;
    }//from  w  w w.j a v a2  s.  co  m
    Document document = null;
    try {
        document = XMLUtil.loadXmlFile(input);
        if (input != null) {
            input.close();
        }
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    Element rootElement = document.getRootElement();
    String id = rootElement.attributeValue("id");
    String version = rootElement.attributeValue("version");
    config.setId(id);
    config.setVersion(version);
    parseGenral(rootElement, config);

    List<Element> preferenceElementList = rootElement.elements("preference");
    List<Preference> preferences = parsePreference(preferenceElementList);
    config.getPreferences().addAll(preferences);

    List<Element> accessElementList = rootElement.elements("access");
    List<Access> accesses = parseAccess(accessElementList);
    config.getAccesses().addAll(accesses);

    List<Element> permissionElementList = rootElement.elements("permission");
    List<Permission> permissions = parsePermission(permissionElementList);
    config.getPermissions().addAll(permissions);

    List<Element> featureElementList = rootElement.elements("feature");
    List<Feature> features = parseFeature(featureElementList);
    config.getFeatures().addAll(features);

    return config;
}

From source file:com.apicloud.commons.model.Feature.java

License:Open Source License

public static List<Feature> loadXml(File file) {
    if (!file.exists()) {
        return new ArrayList<Feature>();
    }/* w ww  .java2  s.co  m*/
    Document document = null;
    try {
        document = XMLUtil.loadXmlFile(file);
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }
    Element rootElement = document.getRootElement();
    List<Element> featureElementList = rootElement.elements("feature");
    return parseFeature(featureElementList);
}

From source file:com.apicloud.commons.model.Feature.java

License:Open Source License

public static List<Feature> loadXml2(File file) {
    if (!file.exists()) {
        return new ArrayList<Feature>();
    }//from  w  w  w.  j  av  a  2  s  .  c om
    Document document = null;
    try {
        document = XMLUtil.loadXmlFile(file);
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }
    Element rootElement = document.getRootElement();
    List<Element> featureElementList = rootElement.elements("feature");
    return parseFeature2(featureElementList);
}

From source file:com.apicloud.commons.model.Feature.java

License:Open Source License

public static List<Feature> loadXml3(File file) {
    if (!file.exists()) {
        return new ArrayList<Feature>();
    }//from www. j  av  a2 s.c  o m
    Document document = null;
    try {
        document = XMLUtil.loadXmlFile(file);
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }
    Element rootElement = document.getRootElement();
    List<Element> featureElementList = rootElement.elements("feature");
    return parseFeature3(featureElementList);
}

From source file:com.app.buzz.weixin.util.XmlUtils.java

License:Open Source License

/**
 * ??xml ?map//from w w  w .j a  v a 2s  .c  o m
 * */
public static Map<String, String> xml2Map(InputStream in) {
    Map<String, String> map = new HashMap<String, String>();
    SAXReader reader = new SAXReader();
    try {
        Document document = reader.read(in);
        Element root = document.getRootElement();
        List<Element> elements = root.elements();
        for (Element e : elements) {
            map.put(e.getName(), e.getText());
        }
        return map;

    } catch (DocumentException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.appdynamics.monitors.hadoop.parser.Parser.java

License:Apache License

/**
 * Parses XML file at <code>xml</code> and collect filtering rules.
 *
 * @param xml//from   www.j a  va2 s . c o m
 * @throws DocumentException
 */
public void parseXML(String xml) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document doc = reader.read(xml);
    Element root = doc.getRootElement();
    String text;

    Iterator<Element> hrmIter = root.element("hadoop-resource-manager").elementIterator();
    Iterator<Element> ambariIter = root.element("ambari").elementIterator();

    while (hrmIter.hasNext()) {
        Element element = hrmIter.next();
        if (element.getName().equals("aggregate-app-period")) {
            if (!(text = element.getText()).equals("")) {
                try {
                    aggrAppPeriod = Integer.parseInt(text);
                } catch (NumberFormatException e) {
                    logger.error("Error parsing aggregate-app-period: " + e + "\n"
                            + "Using default value instead: " + DEFAULT_THREAD_LIMIT);
                    aggrAppPeriod = DEFAULT_AGGR_APP_PERIOD;
                }
            }
        } else if (element.getName().equals("exclude-nodeid")) {
            if (!(text = element.getText()).equals("")) {
                String[] nodeId = text.split(",");
                excludeNodeid.addAll(Arrays.asList(nodeId));
            }
        } else {
            logger.error("Unknown element '" + element.getName() + "' in properties file");
        }
    }

    while (ambariIter.hasNext()) {
        Element element = ambariIter.next();

        if (element.getName().equals("thread-limit")) {
            if (!(text = element.getText()).equals("")) {
                try {
                    threadLimit = Integer.parseInt(text);
                } catch (NumberFormatException e) {
                    logger.error("Error parsing thread-limit " + e + "\n" + "Using default value instead: "
                            + DEFAULT_THREAD_LIMIT);
                    threadLimit = DEFAULT_THREAD_LIMIT;
                }
            }
        } else if (element.getName().equals("include-cluster")) {
            if (!(text = element.getText()).equals("")) {
                String[] appId = text.split(",");
                includeAmbariCluster.addAll(Arrays.asList(appId));
            }
        } else if (element.getName().equals("include-host")) {
            if (!(text = element.getText()).equals("")) {
                String[] appId = text.split(",");
                includeAmbariHost.addAll(Arrays.asList(appId));
            }
        } else if (element.getName().equals("exclude-host")) {
            if (!(text = element.getText()).equals("")) {
                String[] appId = text.split(",");
                excludeAmbariHost.addAll(Arrays.asList(appId));
            }
        } else if (element.getName().equals("exclude-service")) {
            if (!(text = element.getText()).equals("")) {
                String[] appId = text.toLowerCase().split(",");
                excludeAmbariService.addAll(Arrays.asList(appId));
            }
        } else if (element.getName().equals("exclude-service-component")) {
            if (!(text = element.getText()).equals("")) {
                String[] appId = text.toLowerCase().split(",");
                excludeAmbariServiceComponent.addAll(Arrays.asList(appId));
            }
        } else if (element.getName().equals("include-host-metrics")) {
            if (!(text = element.getText()).equals("")) {
                String[] appId = text.toLowerCase().split(",");
                includeAmbariHostMetrics.addAll(Arrays.asList(appId));
            }
        } else if (element.getName().equals("include-component-metrics")) {
            if (!(text = element.getText()).equals("")) {
                String[] appId = text.toLowerCase().split(",");
                includeAmbariComponentMetrics.addAll(Arrays.asList(appId));
            }
        }
    }
}

From source file:com.appdynamics.monitors.hbase.HBaseMonitor.java

License:Apache License

private void getCredentials(final Map<String, String> args) {
    credentials = new ArrayList<Credential>();
    Credential cred = new Credential();

    cred.dbname = args.get("dbname");
    cred.host = args.get("host");
    cred.port = args.get("port");
    cred.username = args.get("user");
    cred.password = args.get("pass");

    if (!isNotEmpty(cred.dbname)) {
        cred.dbname = "DB 1";
    }//from  www  . jav a 2 s .co  m

    credentials.add(cred);

    String xmlPath = args.get("properties-path");
    if (isNotEmpty(xmlPath)) {
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(xmlPath);
            Element root = doc.getRootElement();

            for (Element credElem : (List<Element>) root.elements("credentials")) {
                cred = new Credential();
                cred.dbname = credElem.elementText("dbname");
                cred.host = credElem.elementText("host");
                cred.port = credElem.elementText("port");
                cred.username = credElem.elementText("user");
                cred.password = credElem.elementText("pass");

                if (isNotEmpty(cred.host) && isNotEmpty(cred.port)) {
                    if (!isNotEmpty(cred.dbname)) {
                        cred.dbname = "DB " + (credentials.size() + 1);
                    }
                    credentials.add(cred);
                }
            }
        } catch (DocumentException e) {
            logger.error("Cannot read '" + xmlPath + "'. Monitor is running without additional credentials");
        }
    }
}

From source file:com.appdynamics.snmp.SNMPTrapSender.java

License:Apache License

/**
 * Parses the config xml//  w w w . j  a  v  a 2 s  .com
 * @param    xml         Configuration file locations
 * @return            Map<String, String> - Map of config objects
 * @throws             DocumentException
 */
private static Map<String, String> parseXML(String xml) throws DocumentException {
    Map<String, String> map = new HashMap<String, String>();
    SAXReader reader = new SAXReader();
    Document document = reader.read(xml);
    Element root = document.getRootElement();

    for (Iterator<Element> i = root.elementIterator(); i.hasNext();) {
        Element element = (Element) i.next();
        if (element.getName().equals("snmp-v3")) {
            Iterator<Element> elementIterator = element.elementIterator();
            for (Iterator<Element> j = elementIterator; j.hasNext();) {
                element = (Element) j.next();
                map.put(element.getName(), element.getText());
            }
        } else {
            map.put(element.getName(), element.getText());
        }
    }

    return map;
}

From source file:com.appleframework.pay.trade.utils.WeiXinPayUtils.java

License:Apache License

/**
 * ??xml?,?//from w  ww. j  av a 2 s .  c o m
 * @param requestUrl
 * @param requestMethod
 * @param xmlStr
 * @return
 */
public static Map<String, Object> httpXmlRequest(String requestUrl, String requestMethod, String xmlStr) {
    // ?HashMap
    Map<String, Object> map = new HashMap<String, Object>();
    try {
        HttpsURLConnection urlCon = (HttpsURLConnection) (new URL(requestUrl)).openConnection();
        urlCon.setDoInput(true);
        urlCon.setDoOutput(true);
        // ?GET/POST
        urlCon.setRequestMethod(requestMethod);

        if ("GET".equalsIgnoreCase(requestMethod)) {
            urlCon.connect();
        }

        urlCon.setRequestProperty("Content-Length", String.valueOf(xmlStr.getBytes().length));
        urlCon.setUseCaches(false);
        // gbk?????
        if (null != xmlStr) {
            OutputStream outputStream = urlCon.getOutputStream();
            outputStream.write(xmlStr.getBytes("UTF-8"));
            outputStream.flush();
            outputStream.close();
        }
        InputStream inputStream = urlCon.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        // ??
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStreamReader);
        // xml
        Element root = document.getRootElement();
        // ?
        @SuppressWarnings("unchecked")
        List<Element> elementList = root.elements();
        // ???
        for (Element e : elementList) {
            map.put(e.getName(), e.getText());
        }
        inputStreamReader.close();
        inputStream.close();
        inputStream = null;
        urlCon.disconnect();
    } catch (MalformedURLException e) {
        LOG.error(e.getMessage());
    } catch (IOException e) {
        LOG.error(e.getMessage());
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }
    return map;
}

From source file:com.appleframework.pay.trade.utils.WeiXinPayUtils.java

License:Apache License

/**
 * ???XML/*  ww  w .j av a  2s.  c  o m*/
 *
 * @param inputStream
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(InputStream inputStream) throws Exception {

    if (inputStream == null) {
        return null;
    }

    Map<String, String> map = new HashMap<String, String>();// ?HashMap
    SAXReader reader = new SAXReader();// ??
    Document document = reader.read(inputStream);
    Element root = document.getRootElement();// xml
    List<Element> elementList = root.elements();// ?
    for (Element e : elementList) { // ???
        map.put(e.getName(), e.getText());
    }

    inputStream.close(); // ?
    inputStream = null;

    return map;
}