सभी उत्तर/सुझाव के लिए धन्यवाद! मैंने ऊपर दिए गए कुछ एक्सएमएल पुस्तकालयों की कोशिश की और Simple एक्सएमएल लाइब्रेरी के साथ जाने का फैसला किया। मुझे सभी तत्वों के माध्यम से लूपिंग से बचने के लिए "शब्दकोश" उपयोगिता वर्ग विशेष रूप से उपयोगी पाया गया। सुरुचिपूर्ण और सरल :)
नीचे मैं इसका उपयोग कैसे करता हूं। मुझे आशा है कि यह किसी और में मदद कर सकते हैं ...
सादर,
एलेक्स
एक काम कर उदाहरण (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"));
}
}
कंसोल खिड़की:
A-001
B-001
संसाधन.जावा
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<Resource> resources = new Dictionary<Resource>();
public Resources(@ElementList(entry = "resource", inline = true) Dictionary<Resource> 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<Property> properties;
public Resource(
@Attribute(name = "name") String name,
@ElementList(inline=true, name="property") Dictionary<Property> 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
<resources>
<resource name="res001">
<property name="propA" value="A-001" />
<property name="propB" value="B-001" />
</resource>
<resource name="res002">
<property name="propA" value="A-002" />
<property name="propB" value="B-002" />
</resource>
<resource name="res003">
<property name="propA" value="A-003" />
<property name="propB" value="B-003" />
</resource>
</resources>
थोड़ा विषय बंद
, लेकिन कोई विशेष कारण है कि आप व्यक्ति के रूप में अपनी संपत्ति की दुकान है तत्वों और गुणों के रूप में नहीं? '<संसाधन आईडी =" res003 "propE =" ई "propf =" एफ "/>' –
मैं उस प्रारूप को पहचान नहीं पा रहा हूं। यह क्या है? –
एमिल, यह इस तरह से सबसे प्राकृतिक लगता है। कोई अन्य सुझाव? – etxalpo