Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse xml to hashmap?

Tags:

java

xml

I have an example of an xml I want to parse

 <?xml version="1.0" encoding="utf-8"?>

<Details>
    <detail-a>

        <detail> attribute 1 of detail a </detail>
        <detail> attribute 2 of detail a </detail>
        <detail> attribute 3 of detail a </detail>

    </detail-a>

    <detail-b>
        <detail> attribute 1 of detail b </detail>
        <detail> attribute 2 of detail b </detail>

    </detail-b>


</Details>

I would like from this xml to write a method that will parse it to hashmap that the key is a string and the value is a list of strings.

for instance : key "detail a" value={"attribute 1 of detail a","attribute 2 of detail a","attribute 3 of detail a"}

and so on..

what is the best way to do this ? because I got confused :\

I got this far to try to print detail-a and detail-b but I get blank...

public static void main(String[] args) {
    // TODO Auto-generated method stub

    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            File f= new File("src/Details.xml");
            Document doc=builder.parse(f);
            Element root=doc.getDocumentElement();
            NodeList children=root.getChildNodes();
            for(int i=0;i<children.getLength();i++)
            {
                Node child=children.item(i);
                if (child instanceof Element)
                {
                    Element childElement=(Element) child;
                    Text textNode=(Text)childElement.getFirstChild();
                    String text=textNode.getData().trim();
                    System.out.println(text);

                }
            }

        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();   
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
like image 904
yanivshe Avatar asked Dec 18 '14 13:12

yanivshe


1 Answers

Underscore-java library has a static method U.fromXmlMap(xmlstring). Live example

import com.github.underscore.U;
import java.util.Map;

public class Main {

  public static void main(String[] args) {

    Map<String, Object> map = U.fromXmlMap(
        "<Details>\r\n" + 
        "    <detail-a>\r\n" + 
        "\r\n" + 
        "        <detail> attribute 1 of detail a </detail>\r\n" + 
        "        <detail> attribute 2 of detail a </detail>\r\n" + 
        "        <detail> attribute 3 of detail a </detail>\r\n" + 
        "\r\n" + 
        "    </detail-a>\r\n" + 
        "\r\n" + 
        "    <detail-b>\r\n" + 
        "        <detail> attribute 1 of detail b </detail>\r\n" + 
        "        <detail> attribute 2 of detail b </detail>\r\n" + 
        "\r\n" + 
        "    </detail-b>\r\n" + 
        "\r\n" + 
        "\r\n" + 
        "</Details>");
    
    System.out.println(map);
    // {Details={detail-a={detail=[ attribute 1 of detail a ,  attribute 2 of detail a ,  attribute 3 of detail a ]},
    // detail-b={detail=[ attribute 1 of detail b ,  attribute 2 of detail b ]}}, #omit-xml-declaration=yes}
  }
}
like image 117
Valentyn Kolesnikov Avatar answered Oct 01 '22 01:10

Valentyn Kolesnikov