Example usage for org.w3c.dom Element getElementsByTagName

List of usage examples for org.w3c.dom Element getElementsByTagName

Introduction

In this page you can find the example usage for org.w3c.dom Element getElementsByTagName.

Prototype

public NodeList getElementsByTagName(String name);

Source Link

Document

Returns a NodeList of all descendant Elements with a given tag name, in document order.

Usage

From source file:com.cloud.test.utils.UtilsForTest.java

public static Map<String, String> parseXML(InputStream is, String[] tagNames) {
    Map<String, String> returnValues = new HashMap<String, String>();
    try {//from   w  ww .j av  a2  s .c o m
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);
        Element rootElement = doc.getDocumentElement();

        for (int i = 0; i < tagNames.length; i++) {
            NodeList targetNodes = rootElement.getElementsByTagName(tagNames[i]);
            if (targetNodes.getLength() <= 0) {
                System.out.println("no " + tagNames[i] + " tag in the response");
                returnValues.put(tagNames[i], null);
            } else {
                returnValues.put(tagNames[i], targetNodes.item(0).getTextContent());
            }
        }
    } catch (Exception ex) {
        System.out.println("error processing XML");
        ex.printStackTrace();
    }
    return returnValues;
}

From source file:com.cloud.test.utils.UtilsForTest.java

public static Map<String, List<String>> getMultipleValuesFromXML(InputStream is, String[] tagNames) {
    Map<String, List<String>> returnValues = new HashMap<String, List<String>>();
    try {//from w  w w  . j a  v a  2  s . co m
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);
        Element rootElement = doc.getDocumentElement();
        for (int i = 0; i < tagNames.length; i++) {
            NodeList targetNodes = rootElement.getElementsByTagName(tagNames[i]);
            if (targetNodes.getLength() <= 0) {
                System.out.println("no " + tagNames[i] + " tag in XML response...returning null");
            } else {
                List<String> valueList = new ArrayList<String>();
                for (int j = 0; j < targetNodes.getLength(); j++) {
                    Node node = targetNodes.item(j);
                    valueList.add(node.getTextContent());
                }
                returnValues.put(tagNames[i], valueList);
            }
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }
    return returnValues;
}

From source file:flink.iso8583.parse.ConfigParser.java

/** Reads the XML from the stream and configures the message factory with its values.
 * @param mfact The message factory to be configured with the values read from the XML.
 * @param stream The InputStream containing the XML configuration. */
protected static void parse(MessageFactory mfact, InputStream stream) throws IOException {
    final DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();
    DocumentBuilder docb = null;// www  . jav  a 2 s .c  o m
    Document doc = null;
    try {
        docb = docfact.newDocumentBuilder();
        doc = docb.parse(stream);
    } catch (ParserConfigurationException ex) {
        log.error("Cannot parse XML configuration", ex);
        return;
    } catch (SAXException ex) {
        log.error("Parsing XML configuration", ex);
        return;
    }
    final Element root = doc.getDocumentElement();

    //Read the ISO headers
    NodeList nodes = root.getElementsByTagName("header");
    Element elem = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for header: " + elem.getAttribute("type"));
        }
        if (elem.getChildNodes() == null || elem.getChildNodes().getLength() == 0) {
            throw new IOException("Invalid header element");
        }
        String header = elem.getChildNodes().item(0).getNodeValue();
        if (log.isTraceEnabled()) {
            log.trace("Adding ISO header for type " + elem.getAttribute("type") + ": " + header);
        }
        mfact.setIsoHeader(type, header);
    }

    //Read the message templates
    nodes = root.getElementsByTagName("template");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for template: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        IsoMessage m = new IsoMessage();
        m.setType(type);
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            int num = Integer.parseInt(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            String v = f.getChildNodes().item(0).getNodeValue();
            m.setValue(num, v, itype, length);
        }
        mfact.addMessageTemplate(m);
    }

    //Read the parsing guides
    nodes = root.getElementsByTagName("parse");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for parse guide: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        HashMap parseMap = new HashMap();
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            Integer num = new Integer(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            parseMap.put(num, new FieldParseInfo(itype, length));
        }
        mfact.setParseMap(new Integer(type), parseMap);
    }

}

From source file:com.cloud.test.utils.UtilsForTest.java

public static ArrayList<HashMap<String, String>> parseMulXML(InputStream is, String[] tagNames) {
    ArrayList<HashMap<String, String>> returnValues = new ArrayList<HashMap<String, String>>();

    try {/*  w ww .  ja  v  a 2 s.  co  m*/
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.parse(is);
        Element rootElement = doc.getDocumentElement();
        for (int i = 0; i < tagNames.length; i++) {
            NodeList targetNodes = rootElement.getElementsByTagName(tagNames[i]);
            if (targetNodes.getLength() <= 0) {
                System.out.println("no " + tagNames[i] + " tag in XML response...returning null");
            } else {
                for (int j = 0; j < targetNodes.getLength(); j++) {
                    HashMap<String, String> valueList = new HashMap<String, String>();
                    Node node = targetNodes.item(j);
                    //parse child nodes
                    NodeList child = node.getChildNodes();
                    for (int c = 0; c < node.getChildNodes().getLength(); c++) {
                        child.item(c).getNodeName();
                        valueList.put(child.item(c).getNodeName(), child.item(c).getTextContent());
                    }
                    returnValues.add(valueList);
                }

            }
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }

    return returnValues;
}

From source file:com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser.java

private static String getAttrValue(String tag, String attr, Element eElement) {
    NodeList tagNode = eElement.getElementsByTagName(tag);
    if (tagNode.getLength() == 0) {
        return null;
    }//  w  ww  .  j a v  a  2 s  .  com
    Element node = (Element) tagNode.item(0);
    return node.getAttribute(attr);
}

From source file:com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser.java

private static String getTagValue(String tag, Element eElement) {
    NodeList tagNodeList = eElement.getElementsByTagName(tag);
    if (tagNodeList == null || tagNodeList.getLength() == 0) {
        return null;
    }/*from   www.j a  v a  2s .  c o  m*/

    NodeList nlList = tagNodeList.item(0).getChildNodes();

    Node nValue = nlList.item(0);

    return nValue.getNodeValue();
}

From source file:eu.stork.peps.configuration.ConfigurationReader.java

/**
 * Generate parameters.//  w  ww . j a v a  2  s  .c o m
 * 
 * @param configurationNode the configuration node
 * 
 * @return the map< string, string>
 */
private static Map<String, String> generateParam(final Element configurationNode) {

    final HashMap<String, String> parameters = new HashMap<String, String>();

    final NodeList parameterNodes = configurationNode.getElementsByTagName(NODE_PARAMETER);

    String parameterName;
    String parameterValue;

    for (int k = 0; k < parameterNodes.getLength(); ++k) {
        // for every parameter find, process.
        final Element parameterNode = (Element) parameterNodes.item(k);
        parameterName = parameterNode.getAttribute(NODE_PARAM_NAME);
        parameterValue = parameterNode.getAttribute(NODE_PARAM_VALUE);

        // verified the content.
        if (StringUtils.isBlank(parameterName) || StringUtils.isBlank(parameterValue)) {
            throw new STORKSAMLEngineRuntimeException("Error reader parameters (name - value).");
        } else {
            parameters.put(parameterName.trim(), parameterValue.trim());
        }
    }
    return parameters;
}

From source file:Main.java

/**
 * Find the xpath element under the given root. As the xpath requirement is very
 * simple, so we avoid using XPath API//from   ww  w.j  a  v a2 s  .c o  m
 * @param root - the root element that search begins
 * @param xpath - the path from the root
 * @return - the xpath defined element under the given root.
 */
public static Element findXPathElement(Element root, String xpath) {
    if (root == null)
        return null;
    if (xpath == null || xpath.trim().equals(""))
        return root;
    xpath = toRelativePath(xpath, root.getNodeName());
    StringTokenizer st = new StringTokenizer(xpath, "/", false);
    String sitem;
    Element item = root;
    NodeList list;

    boolean first = true;
    while (st.hasMoreTokens()) {
        sitem = st.nextToken();
        if (first && sitem.equals(item.getNodeName())) {
            first = false;
        } else {
            list = item.getElementsByTagName(sitem);
            if (list.getLength() < 1)
                return null;
            item = (Element) (list.item(0));
        }
    }

    return item;
}

From source file:de.itomig.itoplib.GetItopData.java

private static ArrayList<Organization> parseOrganizationDoc(Document doc) {
    // get the root elememt
    Element root = doc.getDocumentElement();
    NodeList items = root.getElementsByTagName("Organization");

    organizations.clear();//from  w w w. j  a  va  2  s.  c o  m
    for (int i = 0; i < items.getLength(); i++) {
        Node item = items.item(i);
        String org_id = ((Element) items.item(i)).getAttribute("id");
        NodeList properties = item.getChildNodes();
        String org_name = "";
        for (int j = 0; j < properties.getLength(); j++) {
            Node property = properties.item(j);
            String name = property.getNodeName();
            if (name.equalsIgnoreCase("name")) {
                org_name = getConcatNodeValues(property);
            }
        }
        // there should ALLWAYS be an id.

        int id;
        try {
            if (org_name != "") {
                id = Integer.parseInt(org_id);
                organizations.add(new Organization(id, org_name));
            } else {
                Log.e(TAG, "no name for org with id=" + org_id);
            }
        } catch (NumberFormatException e) {
            Log.e("TAG", "id of person not parseable");
        }
    }
    return organizations;
}

From source file:com.moadbus.banking.iso.core.protocol.ConfigParser.java

/**
 * Reads the XML from the stream and configures the message factory with its
 * values.//from  w  ww  .  j a  va  2s.  co m
 *
 * @param mfact
 *            The message factory to be configured with the values read from
 *            the XML.
 * @param stream
 *            The InputStream containing the XML configuration.
 */
protected static void parse(MessageFactory mfact, InputStream stream) throws IOException {
    final DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance();
    DocumentBuilder docb = null;
    Document doc = null;
    try {
        docb = docfact.newDocumentBuilder();
        doc = docb.parse(stream);
    } catch (ParserConfigurationException ex) {
        log.error("parse: Cannot parse XML configuration", ex);
        return;
    } catch (SAXException ex) {
        log.error("parse: Parsing XML configuration", ex);
        return;
    }
    final Element root = doc.getDocumentElement();

    // Read the ISO headers
    NodeList nodes = root.getElementsByTagName("header");
    Element elem = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for header: " + elem.getAttribute("type"));
        }
        if (elem.getChildNodes() == null || elem.getChildNodes().getLength() == 0) {
            throw new IOException("Invalid header element");
        }
        String header = elem.getChildNodes().item(0).getNodeValue();
        if (log.isTraceEnabled()) {
            log.trace("Adding ISO header for type " + elem.getAttribute("type") + ": " + header);
        }
        mfact.setIsoHeader(type, header);
    }

    // Read the message templates
    nodes = root.getElementsByTagName("template");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for template: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        IsoMessage m = new IsoMessage();
        m.setType(type);
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            int num = Integer.parseInt(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            String v = f.getChildNodes().item(0).getNodeValue();
            m.setValue(num, v, itype, length);
        }
        mfact.addMessageTemplate(m);
    }

    // Read the parsing guides
    nodes = root.getElementsByTagName("parse");
    for (int i = 0; i < nodes.getLength(); i++) {
        elem = (Element) nodes.item(i);
        int type = parseType(elem.getAttribute("type"));
        if (type == -1) {
            throw new IOException("Invalid type for parse guide: " + elem.getAttribute("type"));
        }
        NodeList fields = elem.getElementsByTagName("field");
        HashMap<Integer, FieldParseInfo> parseMap = new HashMap<Integer, FieldParseInfo>();
        for (int j = 0; j < fields.getLength(); j++) {
            Element f = (Element) fields.item(j);
            int num = Integer.parseInt(f.getAttribute("num"));
            IsoType itype = IsoType.valueOf(f.getAttribute("type"));
            int length = 0;
            if (f.getAttribute("length").length() > 0) {
                length = Integer.parseInt(f.getAttribute("length"));
            }
            parseMap.put(num, new FieldParseInfo(itype, length));
        }
        mfact.setParseMap(type, parseMap);
    }

}