4

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Azfar
  • 317
  • 1
  • 4
  • 14
  • 2
    @Andreas_D: I really, really tried - Digester seemed simple and fast to use, and we tried to use it in one of our projects for simple XML parsing. It took us a few days to develop appropriate rules for digester. And eventually it turned out that usage of JAXB was simpler to write and it took 5 minutes. However, maybe there are cases were CD is helpful and efficient. – Lukasz Feb 15 '12 at 11:30
  • Thanks! Never used it myself but it looked promising. – Andreas Dolk Feb 15 '12 at 11:32

5 Answers5

3

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);
  }

}
Lukasz
  • 7,572
  • 4
  • 41
  • 50
1
    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>
dg_no_9
  • 1,005
  • 1
  • 10
  • 25
1
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

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
Azfar
  • 317
  • 1
  • 4
  • 14
0

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);
    
Suresh
  • 1,494
  • 3
  • 13
  • 14
  • 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"); – Azfar Feb 15 '12 at 12:32
  • @user1211036: on SO, we publish the correct answer (and mark it as Accepted) – Lukasz Feb 15 '12 at 12:49
  • @user1211036 you may be right.I give my suggestion to you.I already implemented that kind of xml.I use castor mapping.May be in future it is useful for you.Thats why i wrote whole step for castor mapping. – Suresh Feb 15 '12 at 13:36
  • @Azfar: I meant: http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Lukasz Feb 15 '12 at 15:06
-1

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

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

Billybonks
  • 1,568
  • 3
  • 15
  • 32