Azfar
Azfar

Reputation: 317

Parse a map in digester

I have a string of XML as

<ShowPercentage>     
    <SPGLevel>       
        <Level> 1 </Level>   
        <percentage>40</percentage>      
    </SPGLevel>
    <SPGLevel>       
        <Level> 2 </Level>
        <percentage>60</percentage>      
    </SPGLevel>
</ShowPercentage>

The SPG Level is repeatable. This is a map with key=Level and value=percentage

I want to parse this XML using Digester. Can anyone help me get started?

Upvotes: 4

Views: 1509

Answers (5)

dg_no_9
dg_no_9

Reputation: 1013

    final String root= "root/";
    final String out= "elementOut/";
    final String in= "elementIn/";
    final String id = "id";
    final String NAME = "name";

    String namespace = reg.getFilename();
    final Digester digester = new Digester();
    digester.setValidating(false);

    digester.addObjectCreate(root, HashMap.class);
    digester.addCallMethod(root+ out, "put", 2);
    digester.addCallParam(root+ out, 0, ID);
    digester.addObjectCreate(root+ out, ArrayList.class);
    digester.addCallMethod(root+ out + in, "add", 1);
    digester.addCallParam(root+ out + in, 0, name);
    digester.addCallParam(root+ out, 1, true);
    Map<String, List<String>> map = digester.<Map<String, List<String>>> parse(reg
                .getInputStream());

corresponding xml matches map of key as a string and value as list of string.

 <root>
    <elementOut id="key1">
   <elementIn name="value1" />
       </elementOut>
   <elementOutid="key2">
   <elementIn name="value1" />
   <elementIn name="value2" />
   </elementOut>
</root>

Upvotes: 1

Azfar
Azfar

Reputation: 317

digester.addObjectCreate("MerchRecomExitPopupControl/ShowPercentage", HashMap.class);
digester.addCallMethod("MerchRecomExitPopupControl/ShowPercentage/SPGLevel", "put", 2);
digester.addCallParam("MerchRecomExitPopupControl/ShowPercentage/SPGLevel/Level", 0);
digester.addCallParam("MerchRecomExitPopupControl/ShowPercentage/SPGLevel/Percentage", 1);
digester.addSetNext("MerchRecomExitPopupControl/ShowPercentage", "setShowPercentMap");

setShowPercentMap - set this in a helper class, Map of strings

Upvotes: 1

Lukasz
Lukasz

Reputation: 7662

The answer may look like the following code, but I didn't test this:

public class SampleDigester
{
  private Map<String, String> map = new HashMap<String, String>();

  public void run() throws IOException, SAXException {    
    Digester digester = new Digester();
    digester.push(this);

    digester.addCallMethod("ShowPercentage/SPGLevel", "addKey", 2);
    digester.addCallParam("ShowPercentage/SPGLevel/Level", 0);
    digester.addCallParam("ShowPercentage/SPGLevel/percentage", 1);
    digester.parse("input.xml");

  }

  public void addKey(String key, String value) {
    map.put(key, value);
  }

}

Upvotes: 3

Suresh
Suresh

Reputation: 1504

I should suggest that you should go with castor mapping.

Castor mapping

For Castor mapping you should follow below steps:

  1. write your xml file.
  2. make mapping.xml file which is according to your xml file.
  3. Make Pojo according to your main xml file.
  4. After all done write down below code for unmarshalling.

    private String fileLoadPath="conf/Configuration.xml";
    private String mappingPath="conf/mapping.xml";
    
     Mapping mapping = new Mapping();
     mapping.loadMapping(mappingPath);
    
    Configuration configuration = (Configuration)new Unmarshaller(mapping).unmarshal(fileLoadPath);
    

Upvotes: 0

Billybonks
Billybonks

Reputation: 1568

might not be exactly what you want, but will get you started

http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript

Upvotes: -1

Related Questions