Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在digester中解析映射_Java_Xml_Apache Commons Digester - Fatal编程技术网

Java 在digester中解析映射

Java 在digester中解析映射,java,xml,apache-commons-digester,Java,Xml,Apache Commons Digester,我有一个XML字符串作为 <ShowPercentage> <SPGLevel> <Level> 1 </Level> <percentage>40</percentage> </SPGLevel> <SPGLevel> <Level> 2 </Level&

我有一个XML字符串作为

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

1.
40
2.
60
SPG级别是可重复的。这是一张键=级别、值=百分比的地图


我想用Digester解析这个XML。有人能帮我开始吗?

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


我建议您使用castor映射

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

  • 编写xml文件
  • 根据您的xml文件创建mapping.xml文件
  • 根据主xml文件生成Pojo
  • 完成所有操作后,写下以下代码以进行解组

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

  • 答案可能看起来像下面的代码,但我没有测试它:

    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);
      }
    
    }
    
    公共类样本消化器
    {
    私有映射映射=新的HashMap();
    public void run()引发IOException,SAXException{
    蒸煮器蒸煮器=新蒸煮器();
    消化器。推(这个);
    digester.addCallMethod(“ShowPercentage/SPGLevel”,“addKey”,2);
    digester.addCallParam(“显示百分比/标度级别/级别”,0);
    digester.addCallParam(“ShowPercentage/SPGLevel/percentage”,1);
    parse(“input.xml”);
    }
    public void addKey(字符串键、字符串值){
    map.put(键、值);
    }
    }
    
    setShowPercentMap
    -在助手类、字符串映射中设置此属性

    final String root=“root/”;
    
        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());
    
    最终字符串out=“elementOut/”; 最后一个字符串in=“elementIn/”; 最终字符串id=“id”; 最终字符串NAME=“NAME”; String namespace=reg.getFilename(); 最终蒸煮器蒸煮器=新蒸煮器(); 蒸煮器。设置验证(错误); addObjectCreate(根,HashMap.class); 消化器.添加方法(根+出,“放”,2); digester.addCallParam(root+out,0,ID); addObjectCreate(root+out,ArrayList.class); 消化器.添加方法(根+外+内,“添加”,1); digester.addCallParam(root+out+in,0,name); digester.addCallParam(root+out,1,true); Map=消化器。解析(reg .getInputStream());
    相应的xml将键映射匹配为字符串,将值匹配为字符串列表

     <root>
        <elementOut id="key1">
       <elementIn name="value1" />
           </elementOut>
       <elementOutid="key2">
       <elementIn name="value1" />
       <elementIn name="value2" />
       </elementOut>
    </root>
    
    
    
    @Andreas\u D:我真的,真的试过了——Digester似乎使用起来既简单又快速,我们试图在我们的一个项目中使用它来进行简单的XML解析。我们花了几天时间为消化器制定合适的规则。最终证明,JAXB的使用更容易编写,而且只需5分钟。不过,也许有些情况下,我们的CD是有益的和有效的。谢谢!我本人从未使用过它,但它看起来很有前途。digester.addObjectCreate(“MerchRecomExitPopupControl/ShowPercentage”,HashMap.class);消化器添加调用方法(“MerchRecomExitPopupControl/ShowPercentage/SPGLevel”,“put”,2);digester.addCallParam(“MerchRecomExitPopupControl/ShowPercentage/SPGLevel/Level”,0);digester.addCallParam(“MerchRecomExitPopupControl/ShowPercentage/SPGLevel/percentage”,1);digester.addSetNext(“MerchRecomExitPopupControl/ShowPercentage”、“setShowPercentMap”)@user1211036:因此,我们发布了正确答案(并将其标记为已接受)@user1211036您可能是对的。我给您提建议。我已经实现了那种xml。我使用castor映射。可能将来它对您有用。这就是为什么我编写castor映射的整个步骤。@Azfar:我的意思是:
     <root>
        <elementOut id="key1">
       <elementIn name="value1" />
           </elementOut>
       <elementOutid="key2">
       <elementIn name="value1" />
       <elementIn name="value2" />
       </elementOut>
    </root>