4

我有一个 XML 字符串

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

SPG 级别是可重复的。这是一个 key=Level 和 value=percentage 的地图

我想使用 Digester 解析这个 XML。谁能帮我开始?

4

5 回答 5

3

答案可能类似于以下代码,但我没有对此进行测试

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

}
于 2012-02-15T11:24:28.153 回答
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- 将其设置在帮助器类中,字符串映射

于 2012-02-16T07:36:09.707 回答
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());

相应的 xml 将键映射匹配为字符串,将值匹配为字符串列表。

 <root>
    <elementOut id="key1">
   <elementIn name="value1" />
       </elementOut>
   <elementOutid="key2">
   <elementIn name="value1" />
   <elementIn name="value2" />
   </elementOut>
</root>
于 2012-09-04T13:30:13.210 回答
0

我应该建议您使用脚轮映射。

脚轮映射

对于 Castor 映射,您应该遵循以下步骤:

  1. 写你的xml文件。
  2. 根据您的xml文件制作mapping.xml文件。
  3. 根据您的主 xml 文件制作 Pojo。
  4. 完成后写下下面的代码进行解组。

    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);
    
于 2012-02-15T11:21:10.203 回答
-1

可能不是你想要的,但会让你开始

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

于 2012-02-15T11:07:10.070 回答