简单的XML-2个元素,相同名称的不同命名空间

RoD*_*oDo 5 android xml-namespaces xml-parsing simple-framework

我需要将XML字符串解析为对象。我会使用SimpleXML,但是出现错误Duplicate annotation of name 'link' on field 'url' private java.lang.String com.example.rogedormans.xmlreader.XML.Alert.Channel.url

具有相同问题的示例XML:

<rss........>
     <channel>
         <title>The Title</title>
         <link>http://www.someurl.com</link>
         <description>Some description</description>
         <atom:link href="http://dunno.com/rss.xml" rel="self" type="application/rss+xml"/>
        ....
        ....
    </channel>
 </rss>
Run Code Online (Sandbox Code Playgroud)

我GOOGLE了一番,发现这个这个这个计算器的文章,但没有为我工作。我也看过了简单的XML文档,但我不能得到它的工作。

如何将两个“链接”项都放入对象?(我认为这与名称空间有关,但是呢?)

一个代码示例会很好!!!

oll*_*llo 2

您可以通过实施来解决此问题Converter您可以通过为 Channel 类

\n\n
\n\n

这是给您的一个例子。它缺乏任何类型的错误检查等,并且被简化为Channel具有单个Atom。但您会看到它是如何工作的。

\n\n

ChannelConverter

\n\n
@Root()\n@Convert(Channel.ChannelConverter.class) // Specify the Converter\npublic class Channel\n{\n    @Element\n    private String title;\n    @Element\n    private String link;\n    @Element\n    private String description;\n    @Namespace(reference = "http://www.w3.org/2005/Atom", prefix = "atom")\n    @Element()\n    private AtomLink atomLink;\n\n    // Ctor\'s / getter / setter ...\n\n\n    static class ChannelConverter implements Converter<Channel>\n    {\n        @Override\n        public Channel read(InputNode node) throws Exception\n        {\n            Channel channel = new Channel();\n            InputNode child;\n\n            // Iterate over all childs an get their values\n            while( ( child = node.getNext() ) != null )\n            {\n                switch(child.getName())\n                {\n                    case "title":\n                        channel.setTitle(child.getValue());\n                        break;\n                    case "description":\n                        channel.setDescription(child.getValue());\n                        break;\n                    case "link":\n                        /*\n                         * "link" can be either a <link>...</link> or\n                         * a <atom:link>...</atom:link>\n                         */\n                        if( child.getPrefix().equals("atom") == true )\n                        {\n                            AtomLink atom = new AtomLink();\n                            atom.setHref(child.getAttribute("href").getValue());\n                            atom.setRel(child.getAttribute("rel").getValue());\n                            atom.setType(child.getAttribute("type").getValue());\n                            channel.setAtomLink(atom);\n                        }\n                        else\n                        {\n                            channel.setLink(child.getValue());\n                        }\n                        break;\n                    default:\n                        throw new RuntimeException("Unknown Element found: " + child);\n                }\n            }\n\n            return channel;\n        }\n\n\n        @Override\n        public void write(OutputNode node, Channel value) throws Exception\n        {\n            /*\n             * TODO: Implement if necessary\n             */\n            throw new UnsupportedOperationException("Not supported yet.");\n        }\n\n    }\n\n\n    @Root\n    public static class AtomLink\n    {\n        @Attribute\n        private String href;\n        @Attribute\n        private String rel;\n        @Attribute\n        private String type;\n\n        // Ctor\'s / getter / setter ...\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

所有内部类也可以像普通类一样实现。如果序列化(反序列化)很复杂,您可以SerializerConverter

\n\n

用法

\n\n

最后是一个演示:

\n\n
final String xml = "     <channel>\\n"\n        + "         <title>The Title</title>\\n"\n        + "         <link>http://www.someurl.com</link>\\n"\n        + "         <description>Some description</description>\\n"\n        + "         <atom:link href=\\"http://dunno.com/rss.xml\\" rel=\\"self\\" type=\\"application/rss+xml\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" />\\n"\n        + "        ....\\n"\n        + "        ....\\n"\n        + "    </channel>\\n";\n\nSerializer ser = new Persister(new AnnotationStrategy());\n//                             ^----- Important! -----^\nChannel c = ser.read(Channel.class, xml);\nSystem.out.println(c);\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,Converter需要一个Strategy(有关更多详细信息,请参阅上面的链接);我已经使用了,AnnotationStrategy因为你可以简单地使用@Convert()then。必须xmlns在 XML 中的某个位置定义,否则您将捕获异常;我把它放入<atom:link \xe2\x80\xa6 />这里了。

\n\n

输出

\n\n
Channel{title=The Title, link=http://www.someurl.com, description=Some description, atomLink=AtomLink{href=http://dunno.com/rss.xml, rel=self, type=application/rss+xml}}\n
Run Code Online (Sandbox Code Playgroud)\n