Java XML Parse String parseXML(String resp, String name)

Here you can find the source of parseXML(String resp, String name)

Description

parse XML

License

Apache License

Declaration

public static String parseXML(String resp, String name) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.FactoryConfigurationError;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    public static String parseXML(String resp, String name) {
        String value = null;/*  ww w. j  ava 2 s.  c  om*/
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(resp)));
            doc.getDocumentElement().normalize();
            /*System.out.println ("Root element of the doc is " + 
              doc.getDocumentElement().getNodeName());*/
            NodeList listOfProperties = doc.getElementsByTagName("ConfigCategory");
            //         System.out.println ("No. of nodes " + 
            //               listOfProperties.getLength());
            for (int i = 0; i < listOfProperties.getLength(); i++) {

                Node listOfProperty = listOfProperties.item(i);
                if (listOfProperty.getNodeType() == Node.ELEMENT_NODE) {
                    Element firstPropertyElement = (Element) listOfProperty;
                    NodeList list = firstPropertyElement.getElementsByTagName("attribute");
                    //System.out.println("No of attribute Nodes - " + list.getLength());
                    for (int j = 0; j < list.getLength(); j++) {
                        Element PropElement = (Element) list.item(j);

                        String key = PropElement.getAttribute("name");
                        if (key.contentEquals(name))
                            value = PropElement.getAttribute("value");
                        //System.out.println(key + " - " + value);
                    }

                }
            }
        } catch (FactoryConfigurationError e) {
            // unable to get a document builder factory
        } catch (ParserConfigurationException e) {
            // parser was unable to be configured
        } catch (SAXException e) {
            // parsing error
        } catch (IOException e) {

        }
        return value;
    }
}

Related

  1. parseArray(String array)
  2. parseArray(String inputArray)
  3. parseXml(Object obj)
  4. parseXML(String path)
  5. parseXML(String pathToMap)
  6. parseXML(String text)
  7. parseXml(String uri)
  8. parseXml(String xml)
  9. parseXml(String xml)