감사합니다! 위에 주어진 xml 라이브러리 중 일부를 시도하고 Simple XML 라이브러리를 사용하기로 결정했습니다. "Dictionary"유틸리티 클래스가 특히 유용하다는 것을 알았습니다. 모든 요소를 반복하지 않도록했습니다. 우아하고 단순 :)
다음은 내가 사용한 방법입니다.
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
나는 ... 그것은 다른 사람을 도울 수
감사합니다,
알렉스 (Windows Vista에서)
동작하는 예제를 희망
Resources.java
,363,210
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>
약간 주제 오프
,하지만 당신은 개인으로 당신의 재산을 저장하는 특별한 이유가있다 요소가 아니라 속성으로서? ' ' –
그 형식을 인식하지 못합니다. 이게 뭐야? –
에밀, 이런 식으로 가장 자연 스럽습니다. 다른 제안? – etxalpo