Java Code Examples for nu.xom.Element#getFirstChildElement()

The following examples show how to use nu.xom.Element#getFirstChildElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SettingsManager.java    From ciscorouter with MIT License 6 votes vote down vote up
/**
 * Constructs the class. Requires settings.xml to be present to run
 */
public SettingsManager() {
    String currentDir = System.getProperty("user.dir");
    String settingDir = currentDir + "/settings/";
    File f = new File(settingDir + "settings.xml");
    if (f.exists() && !f.isDirectory()) {
        try {
            Builder parser = new Builder();
            Document doc   = parser.build(f);
            Element root   = doc.getRootElement();

            Element eUsername = root.getFirstChildElement("Username");
            username = eUsername.getValue();

            Element ePassword = root.getFirstChildElement("Password");
            password = ePassword.getValue();

            requiresAuth = true;
        } catch (ParsingException|IOException e) {
            e.printStackTrace();
        }
    }
    else {
        requiresAuth = false;
    }
}
 
Example 2
Source File: XmlPersistenceRead.java    From rest-client with Apache License 2.0 5 votes vote down vote up
protected Request xml2Request(final Document doc)
        throws MalformedURLException, XMLException {
    // get the rootNode
    Element rootNode = doc.getRootElement();

    if (!"rest-client".equals(rootNode.getQualifiedName())) {
        throw new XMLException("Root node is not <rest-client>");
    }

    // checking correct rest version
    final String rcVersion = rootNode.getAttributeValue("version");
    try {
        Versions.versionValidCheck(rcVersion);
    }
    catch(Versions.VersionValidationException ex) {
        throw new XMLException(ex);
    }
    
    readVersion = rcVersion;

    // if more than two request element is present then throw the exception 
    if (rootNode.getChildElements().size() != 1) {
        throw new XMLException("There can be only one child node for root node: <request>");
    }
    // minimum one request element is present in xml 
    if (rootNode.getFirstChildElement("request") == null) {
        throw new XMLException("The child node of <rest-client> should be <request>");
    }
    Element requestNode = rootNode.getFirstChildElement("request");
    
    return getRequestBean(requestNode);
}
 
Example 3
Source File: XmlPersistenceRead.java    From rest-client with Apache License 2.0 4 votes vote down vote up
protected Response xml2Response(final Document doc)
        throws XMLException {
    ResponseBean responseBean = new ResponseBean();

    // get the rootNode
    Element rootNode = doc.getRootElement();

    if (!"rest-client".equals(rootNode.getQualifiedName())) {
        throw new XMLException("Root node is not <rest-client>");
    }

    // checking correct rest version
    try {
        Versions.versionValidCheck(rootNode.getAttributeValue("version"));
    }
    catch(Versions.VersionValidationException ex) {
        throw new XMLException(ex);
    }

    // assign rootnode to current node and also finding 'response' node
    Element tNode = null;
    Element responseNode = null;

    // if more than two request element is present then throw the exception
    if (rootNode.getChildElements().size() != 1) {
        throw new XMLException("There can be only one child node for root node: <response>");
    }
    // minimum one response element is present in xml
    if (rootNode.getFirstChildElement("response") == null) {
        throw new XMLException("The child node of <rest-client> should be <response>");
    }
    responseNode = rootNode.getFirstChildElement("response");
    for (int i = 0; i < responseNode.getChildElements().size(); i++) {
        tNode = responseNode.getChildElements().get(i);
        String nodeName = tNode.getQualifiedName();

        if ("execution-time".equals(nodeName)) {
            responseBean.setExecutionTime(Long.parseLong(tNode.getValue()));
        } else if ("status".equals(nodeName)) {
            responseBean.setStatusLine(tNode.getValue());
            responseBean.setStatusCode(Integer.parseInt(tNode.getAttributeValue("code")));
        } else if ("headers".equals(nodeName)) {
            Map<String, String> m = getHeadersFromHeaderNode(tNode);
            for (String key : m.keySet()) {
                responseBean.addHeader(key, m.get(key));
            }
        } else if ("body".equals(nodeName)) {
            final String base64body = tNode.getValue();
            responseBean.setResponseBody(Util.base64decodeByteArray(base64body));
        } else if ("test-result".equals(nodeName)) {
            TestResultBean testResultBean = new TestResultBean();

            for (int j = 0; j < tNode.getChildCount(); j++) {
                String nn = tNode.getQualifiedName();
                if ("run-count".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("failure-count".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("error-count".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("failures".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                } else if ("errors".equals(nn)) {
                    throw new XMLException("<headers> element should contain only <header> elements");
                }
            }
            responseBean.setTestResult(testResultBean);
        } else {
            throw new XMLException("Unrecognized element found: <" + nodeName + ">");
        }
    }
    return responseBean;
}