How to read properties from xml file with java?

后端 未结 7 1325
独厮守ぢ
独厮守ぢ 2021-01-08 00:51

I have the following xml file:


    
        
        

        
7条回答
  •  走了就别回头了
    2021-01-08 01:30

    Thanks for all the answers/suggestions! I tried some of the xml libraries given above and decided to go with the Simple XML library. I found the "Dictionary" utility class especially useful, to avoid looping through all the elements. Elegant and simple :)

    Below is how I used it. I hope it can help someone else...

    Regards,

    Alex

    A working example (on Windows Vista):

    package demo;
    
    import java.io.File;
    
    import org.simpleframework.xml.Serializer;
    import org.simpleframework.xml.core.Persister;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            File file = new File("c:\\temp\\resources.xml");
            Serializer serializer = new Persister();
            Resources resources = serializer.read(Resources.class, file);
    
            Resource resource = resources.getResourceByName("res001");
            System.out.println(resource.getProperty("propA"));
            System.out.println(resource.getProperty("propB"));
        }
    }
    

    Console window:

    A-001
    B-001
    

    Resources.java

    package demo;
    
    import org.simpleframework.xml.ElementList;
    import org.simpleframework.xml.Root;
    import org.simpleframework.xml.util.Dictionary;
    
    @Root(name="resources")
    public class Resources {
        @ElementList(entry = "resource", inline = true) 
        private Dictionary resources = new Dictionary();
    
        public Resources(@ElementList(entry = "resource", inline = true) Dictionary resources) {
            this.resources = resources;
    }
    
        public Resource getResourceByName(String name){
            return resources.get(name);
        }
    }
    

    Resource.java

    package demo;
    
    import org.simpleframework.xml.Attribute;
    import org.simpleframework.xml.ElementList;
    import org.simpleframework.xml.util.Dictionary;
    import org.simpleframework.xml.util.Entry;
    
    public class Resource  implements Entry{
        @Attribute(name = "name") private final String name;
        @ElementList(inline=true, name="property") private Dictionary properties;
    
        public Resource(
                        @Attribute(name = "name") String name, 
                        @ElementList(inline=true, name="property") Dictionary properties) {
                this.name = name;
                this.properties = properties;
        }
    
        public String getName() {
            return name;
        }
    
        public String getProperty(String name) {
            return properties.get(name).getValue();
        }
    }
    

    Property.java

    package demo;
    
    import org.simpleframework.xml.Attribute;
    import org.simpleframework.xml.Root;
    import org.simpleframework.xml.util.Entry;
    
    @Root
    public class Property implements Entry{
        @Attribute(name="name") private String name;
        @Attribute(name="value") private String value;
    
        public Property(@Attribute(name="name") String name, @Attribute(name="value") String value) {
            this.name = name;
            this.value = value;
        }
    
        public String getName() {
            return name;
        }
    
        public String getValue() {
            return value;
        }
    }
    

    resources.xml

    
        
            
            
        
        
            
            
        
        
            
            
        
    
    

提交回复
热议问题