我可以使用 SimpleXML 来解析结构未知的 XML 吗?
Can I use SimpleXML to parse XML whose structure is unknown?
我正在使用 SimpleXML 来解析通信协议中使用的小 XML 文件。这一切都很好,但现在我正在实施协议的一部分,其中包括一种自由格式 XML.
例如,像这样的 XML:
<telegram>
<config>
<foo>yes</foo>
<bar>no</bar>
</config>
</telegram>
其中 foo
和 bar
将来可能会更改,或者可能会添加一个元素 baz
,而无需触及解析代码。我想使用
之类的结构访问 Java 中的这些元素
tree.getConfig().get("bar"); // returns "no"
我可以使用 SimpleXML 来解析吗?我查看了文档,但找不到我需要的内容。
Can I use SimpleXML to parse that?
不是开箱即用的 - 但写一个 Converter 就可以了。
@Root(name = "telegram")
@Convert(Telegram.TelegramConverter.class) // Requires AnnotationStrategy
public class Telegram
{
private Map<String, String> config;
public String get(String name)
{
return config.get(name);
}
public Map<String, String> getConfig()
{
return config;
}
// ...
@Override
public String toString()
{
return "Telegram{" + "config=" + config + '}';
}
static class TelegramConverter implements Converter<Telegram>
{
@Override
public Telegram read(InputNode node) throws Exception
{
Telegram t = new Telegram();
final InputNode config = node.getNext("config");
t.config = new HashMap<>();
// Iterate over config's child nodes and put them into the map
InputNode cfg = config.getNext();
while( cfg != null )
{
t.config.put(cfg.getName(), cfg.getValue());
cfg = config.getNext();
}
return t;
}
@Override
public void write(OutputNode node, Telegram value) throws Exception
{
// Implement if you need serialization too
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
用法:
final String xml = "<telegram>\n"
+ " <config>\n"
+ " <foo>yes</foo>\n"
+ " <bar>no</bar>\n"
+ " <baz>maybe</baz>\n" // Some "future element"
+ " </config>\n"
+ "</telegram>";
/*
* The AnnotationStrategy is set here since it's
* necessary for the @Convert annotation
*/
Serializer ser = new Persister(new AnnotationStrategy());
Telegram t = ser.read(Telegram.class, xml);
System.out.println(t);
结果:
Telegram{config={bar=no, foo=yes, baz=maybe}}
因为@Convert 符号也可以放在字段上,所以我发现了比“ollo”的精彩回答更简洁的代码:
@Root(name = "telegram")
// Requires AnnotationStrategy
public class Telegram {
@Element
@Convert(Telegram.ConfigConverter.class)
private Map<String, String> config;
public String get(String name) {
return config.get(name);
}
public Map<String, String> getConfig() {
return config;
}
// ...
@Override
public String toString() {
return "Telegram{" + "config=" + config + '}';
}
static class ConfigConverter implements Converter<Map<String, String>> {
@Override
public Map<String, String> read(final InputNode configNode) throws Exception {
Map<String, String> map = new HashMap<>();
// Iterate over config's child nodes and put them into the map
InputNode cfg = configNode.getNext();
while (cfg != null) {
map.put(cfg.getName(), cfg.getValue());
cfg = configNode.getNext();
}
return map;
}
@Override
public void write(OutputNode node, Map<String, String> value) throws Exception {
// Implement if you need serialization too
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
和测试:
final String xml = "<telegram>\n"
+ " <config>\n"
+ " <foo>yes</foo>\n"
+ " <bar>no</bar>\n"
+ " <baz>maybe</baz>\n" // Some "future element"
+ " </config>\n"
+ "</telegram>";
/*
* The AnnotationStrategy is set here since it's
* necessary for the @Convert annotation
*/
Serializer ser = new Persister(new AnnotationStrategy());
Telegram t = ser.read(Telegram.class, xml);
assertEquals("yes",t.getConfig().get("foo"));
assertEquals("no",t.getConfig().get("bar"));
assertEquals("maybe",t.getConfig().get("baz"));
assertEquals(3, t.getConfig().size());
我正在使用 SimpleXML 来解析通信协议中使用的小 XML 文件。这一切都很好,但现在我正在实施协议的一部分,其中包括一种自由格式 XML.
例如,像这样的 XML:
<telegram>
<config>
<foo>yes</foo>
<bar>no</bar>
</config>
</telegram>
其中 foo
和 bar
将来可能会更改,或者可能会添加一个元素 baz
,而无需触及解析代码。我想使用
tree.getConfig().get("bar"); // returns "no"
我可以使用 SimpleXML 来解析吗?我查看了文档,但找不到我需要的内容。
Can I use SimpleXML to parse that?
不是开箱即用的 - 但写一个 Converter 就可以了。
@Root(name = "telegram")
@Convert(Telegram.TelegramConverter.class) // Requires AnnotationStrategy
public class Telegram
{
private Map<String, String> config;
public String get(String name)
{
return config.get(name);
}
public Map<String, String> getConfig()
{
return config;
}
// ...
@Override
public String toString()
{
return "Telegram{" + "config=" + config + '}';
}
static class TelegramConverter implements Converter<Telegram>
{
@Override
public Telegram read(InputNode node) throws Exception
{
Telegram t = new Telegram();
final InputNode config = node.getNext("config");
t.config = new HashMap<>();
// Iterate over config's child nodes and put them into the map
InputNode cfg = config.getNext();
while( cfg != null )
{
t.config.put(cfg.getName(), cfg.getValue());
cfg = config.getNext();
}
return t;
}
@Override
public void write(OutputNode node, Telegram value) throws Exception
{
// Implement if you need serialization too
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
用法:
final String xml = "<telegram>\n"
+ " <config>\n"
+ " <foo>yes</foo>\n"
+ " <bar>no</bar>\n"
+ " <baz>maybe</baz>\n" // Some "future element"
+ " </config>\n"
+ "</telegram>";
/*
* The AnnotationStrategy is set here since it's
* necessary for the @Convert annotation
*/
Serializer ser = new Persister(new AnnotationStrategy());
Telegram t = ser.read(Telegram.class, xml);
System.out.println(t);
结果:
Telegram{config={bar=no, foo=yes, baz=maybe}}
因为@Convert 符号也可以放在字段上,所以我发现了比“ollo”的精彩回答更简洁的代码:
@Root(name = "telegram")
// Requires AnnotationStrategy
public class Telegram {
@Element
@Convert(Telegram.ConfigConverter.class)
private Map<String, String> config;
public String get(String name) {
return config.get(name);
}
public Map<String, String> getConfig() {
return config;
}
// ...
@Override
public String toString() {
return "Telegram{" + "config=" + config + '}';
}
static class ConfigConverter implements Converter<Map<String, String>> {
@Override
public Map<String, String> read(final InputNode configNode) throws Exception {
Map<String, String> map = new HashMap<>();
// Iterate over config's child nodes and put them into the map
InputNode cfg = configNode.getNext();
while (cfg != null) {
map.put(cfg.getName(), cfg.getValue());
cfg = configNode.getNext();
}
return map;
}
@Override
public void write(OutputNode node, Map<String, String> value) throws Exception {
// Implement if you need serialization too
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
和测试:
final String xml = "<telegram>\n"
+ " <config>\n"
+ " <foo>yes</foo>\n"
+ " <bar>no</bar>\n"
+ " <baz>maybe</baz>\n" // Some "future element"
+ " </config>\n"
+ "</telegram>";
/*
* The AnnotationStrategy is set here since it's
* necessary for the @Convert annotation
*/
Serializer ser = new Persister(new AnnotationStrategy());
Telegram t = ser.read(Telegram.class, xml);
assertEquals("yes",t.getConfig().get("foo"));
assertEquals("no",t.getConfig().get("bar"));
assertEquals("maybe",t.getConfig().get("baz"));
assertEquals(3, t.getConfig().size());