Java 我可以使用SimpleXML解析结构未知的XML吗?
我正在使用SimpleXML解析通信协议中使用的小XML文件。这一切都很好,但现在我正在实现协议的一部分,其中包括一种自由格式的XML 例如,像这样的XML:Java 我可以使用SimpleXML解析结构未知的XML吗?,java,android,xml,simple-framework,Java,Android,Xml,Simple Framework,我正在使用SimpleXML解析通信协议中使用的小XML文件。这一切都很好,但现在我正在实现协议的一部分,其中包括一种自由格式的XML 例如,像这样的XML: <telegram> <config> <foo>yes</foo> <bar>no</bar> </config> </telegram> 我可以使用SimpleXML解析它吗?我查阅了文档,但找不到我需要的东西 我
<telegram>
<config>
<foo>yes</foo>
<bar>no</bar>
</config>
</telegram>
我可以使用SimpleXML解析它吗?我查阅了文档,但找不到我需要的东西
我可以使用SimpleXML解析它吗
不是开箱即用——但写一篇文章就行了
因为@Convert符号也可以放在字段上,所以我找到了比“ollo”的精彩答案更简洁的代码:
@Root(name=“电报”)
//需要注释策略
公共类电报{
@元素
@转换(电报.ConfigConverter.class)
私有地图配置;
公共字符串获取(字符串名称){
返回config.get(name);
}
公共映射getConfig(){
返回配置;
}
// ...
@凌驾
公共字符串toString(){
返回“电报{“+”config=“+config+'}”;
}
静态类ConfigConverter实现转换器{
@凌驾
公共映射读取(最终InputNode configNode)引发异常{
Map Map=newhashmap();
//迭代config的子节点并将它们放入映射中
InputNode cfg=configNode.getNext();
while(cfg!=null){
put(cfg.getName(),cfg.getValue());
cfg=configNode.getNext();
}
返回图;
}
@凌驾
公共无效写入(OutputNode节点,映射值)引发异常{
//如果还需要序列化,请实现
抛出新的UnsupportedOperationException(“尚未支持”);
}
}
}
以及测试:
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());
final String xml=“\n”
+“\n”
+“是\n”
+“否\n”
+“可能\n”//future元素”
+“\n”
+ "";
/*
*注释策略在此处设置,因为它是
*@Convert注释所必需的
*/
Serializer ser=new Persister(new AnnotationStrategy());
电报t=ser.read(电报类,xml);
assertEquals(“yes”,t.getConfig().get(“foo”);
assertEquals(“no”,t.getConfig().get(“bar”);
assertEquals(“maybe”,t.getConfig().get(“baz”);
assertEquals(3,t.getConfig().size());
@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}}
@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());