-1

I have XML which i want to parse to Map. but due to lack of java knowledge i am not able to do it. My XML format is something like this.

<configuration>    
<config name="name1" type="main" format="xyz-D" size=1 scale=2 required=no/>
<config name="name2" type="sub" format="xyz-E" size=12 scale=5 required=no/>
<config name="name3" type="last" format="xyz-C" size=2 scale=1 required=no/>
<config name="name4" type="first" format="xyz-S" size=16 scale=3 required=no/>
<config name="name5" type="main" format="xyz-S" size=17 scale=2 required=no/>
<config name="name6" type="main" format="xyz-S" size=18 scale=2 required=no/>
<config name="name7" type="first" format="xyz-S" size=23 scale=4 required=no/>
<config name="name8" type="last" format="xyz-S" size=45 scale=2 required=no/>
<config name="name9" type="sub" format="xyz-s" size=39 scale=15 required=no/>
<configuration/> 

how do i convert this to in MAP so i can used it for latter manipulation

UmeshR
  • 803
  • 1
  • 9
  • 16
  • This question might be the right kind of place to start: http://stackoverflow.com/questions/373833/best-xml-parser-for-java (your question is too general I think). – lindon fox May 25 '13 at 15:03
  • Try looking at this link: http://docs.oracle.com/javase/tutorial/jaxp/index.html. For you, I think DOM would be good (http://docs.oracle.com/javase/tutorial/jaxp/dom/index.html). If you think DOM isn't what you're looking for, try taking a look at SAX (http://docs.oracle.com/javase/tutorial/jaxp/sax/index.html). –  May 25 '13 at 15:09
  • i am looking solution to convert given XML to List> is any one knows how to do it? – UmeshR May 25 '13 at 15:33
  • @Java SAX is good for exactly nobody now that we have [StAX](http://docs.oracle.com/javase/7/docs/api/index.html?javax/xml/stream/package-summary.html). Also, unless the input file is *huge*, DOM or [JDOM](http://www.jdom.org/) is the way to go, or maaaybe JAXB but it's arguably overkill for a simple use case like this. – millimoose May 25 '13 at 15:40
  • @millimoose I agree. SAX is pretty simple and isn't very useful now, but I'd imagine that the OP wouldn't want to use a pull parser like StAX to ask for the next event when he is trying to put data into a Map. As you said, DOM or JDOM would be best unless the file is large. –  May 27 '13 at 17:37
  • @Java What I meant is that StAX pretty much obsoletes SAX for the use cases where SAX is required. (I.e. huge inputs that you can process in a streaming fashion, with only a little data that has to be kept in memory.) If you're going to parse XML into a data structure that mirrors it more or less 1:1, DOM only gives you O(n) space overhead, which might or might not be worth the bother. (It's not for config files.) – millimoose May 27 '13 at 17:58

1 Answers1

0

Below is the code that builds Config objects corresponding to each <config> element. Improvise this to add these objects to a List or Map.

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

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;

public class ConfigImporter
{
    private static String cfgXml = "<configuration> <config name='name1' type='main' format='xyz-D' size='1' scale='2' required='no'/> <config name='name2' type='sub' format='xyz-E' size='12' scale='5' required='no'/> <config name='name3' type='last' format='xyz-C' size='2' scale='1' required='no'/> <config name='name4' type='first' format='xyz-S' size='16' scale='3' required='no'/> <config name='name5' type='main' format='xyz-S' size='17' scale='2' required='no'/> <config name='name6' type='main' format='xyz-S' size='18' scale='2' required='no'/> <config name='name7' type='first' format='xyz-S' size='23' scale='4' required='no'/> <config name='name8' type='last' format='xyz-S' size='45' scale='2' required='no'/> <config name='name9' type='sub' format='xyz-s' size='39' scale='15' required='no'/> </configuration>";

    public static void main(String[] args)
    {
        try
        {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new InputSource(new StringReader(cfgXml)));

            // optional, but recommended
            // read this -
            // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("config");

            System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++)
            {

                Node nNode = nList.item(temp);

                System.out.println("\nCurrent Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE)
                {

                    Element eElement = (Element) nNode;

                    Config c = new Config();

                    c.name = eElement.getAttribute("name");
                    c.type = eElement.getAttribute("type");
                    c.format = eElement.getAttribute("format");
                    c.size = Integer.valueOf(eElement.getAttribute("size"));
                    c.scale = Integer.valueOf(eElement.getAttribute("scale"));
                    String attribute = eElement.getAttribute("required");

                    c.required = Boolean.valueOf("Yes".equalsIgnoreCase(attribute) ? true : false);

                    System.out.println("Imported config : " + c);
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static class Config
    {
        @Override
        public String toString()
        {
            return "Config [name=" + name + ", type=" + type + ", format=" + format + ", size=" + size + ", scale=" + scale + ", required="
                    + required + "]";
        }

        public String name;
        public String type;
        public String format;
        public int size;
        public int scale;
        public boolean required;
    }
}
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327