Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

java - Simple Xml - Element Declared Twice Error

I have been trying to wrap a set of classes based on Simple XML (Java Serializer) around a RSS Feed. The sample feed is

<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
    <title>Coding Horror</title>
    <link>http://www.codinghorror.com/blog/</link>
    <description>programming and human factors - Jeff Atwood</description>
    <language>en-us</language>

    <lastBuildDate>Wed, 04 May 2011 20:34:18 -0700</lastBuildDate>
    <pubDate>Wed, 04 May 2011 20:34:18 -0700</pubDate>
    <generator>http://www.typepad.com/</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>

    <image>
        <title>Coding Horror</title>
        <url>http://www.codinghorror.com/blog/images/coding-horror-official-logo-small.png</url>
        <width>100</width>
        <height>91</height>
        <description>Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.</description>
        <link>http://www.codinghorror.com/blog/</link>
    </image>

    <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />   
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />        

</channel>
 </rss>

The error that I am getting while running the code is

org.simpleframework.xml.core.PersistenceException: Element 'link' declared twice at line 24

And the error is fair enough because the particular element name occurs twice in the xml but in different ways.

The first link element is here

<link>http://www.codinghorror.com/blog/</link>

Its directly under the Channel tag. And then the next link tag is again under Channel in the following format

<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />

In Channel.java class I cannot have two variables with the same name link. I tried changing a variable name to blogLink and tried giving name in the Element annotation and Eclipse gave me this error

 Change was

@Element("name=link")


Result is

The attribute value is undefined for the annotation Element

I know I am missing something here but I am not able to put my finger on it. I would appreciate any help on this.

UPDATE

Channel Class

@Element(name="link")
@Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom")
private atomlink atomlink;

public atomlink getAtomLink() {
    return atomlink;
}

Link Class

   import org.simpleframework.xml.Attribute;
   import org.simpleframework.xml.Namespace;
   import org.simpleframework.xml.Root;

  @Root(name="link")
  @Namespace(reference="http://www.w3.org/2005/Atom",prefix="atom10")
  public class atomlink {

@Attribute 
private String rel;

public String getRel() {
    return rel;
}

}

I have changed the class names and yet it still points to the same error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because the link element appears twice(though they have different namespace), you need to tell link apart, blew is my whole answer:

1)Rss bean

package com.example.xyzreader.data.bean;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.NamespaceList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;

import java.util.List;

/**
 * @author zmingchun
 * @version 1.0.0 (2017/3/20)
 */
@NamespaceList({
        @Namespace(prefix = "dc", reference = "http://purl.org/dc/elements/1.1/"),
        @Namespace(prefix = "atom", reference = "http://www.w3.org/2005/Atom"),
        @Namespace(prefix = "xhtml", reference = "http://www.w3.org/1999/xhtml"),
        @Namespace(prefix = "atom10", reference = "http://www.w3.org/2005/Atom"),
})
@Root(name = "rss" , strict = false)
public class Rss {
    @Element(name = "channel")
    public ChannelBean channel;
    @Attribute
    public String version;

    @Override
    public String toString() {
        return "Rss{" +
                "channel=" + channel +
                ", version='" + version + ''' +
                '}';
    }

    @Root(name = "channel", strict = false)
    public static class ChannelBean {
        @Element
        public String title;
        @ElementList(entry = "link", inline = true, required = false)
        public List<Link> links;
        @Element
        public String description;
        @Element
        public String language;
        @Element
        public String lastBuildDate;
        @Element
        public String pubDate;
        @Element
        public String generator;
        @Element
        public String docs;
        @Element
        public ImageBean image;
        @Element
        public MetaBean meta;

        @Override
        public String toString() {
            return "ChannelBean{" +
                    "title='" + title + ''' +
                    ", links=" + links +
                    ", description='" + description + ''' +
                    ", language='" + language + ''' +
                    ", lastBuildDate='" + lastBuildDate + ''' +
                    ", pubDate='" + pubDate + ''' +
                    ", generator='" + generator + ''' +
                    ", docs='" + docs + ''' +
                    ", image=" + image +
                    ", meta=" + meta +
                    '}';
        }

        /**
         * link bean
         */
        static class Link {
            @Attribute(required = false)
            public String href;

            @Attribute(required = false)
            public String rel;

            @Attribute(name = "type", required = false)
            public String contentType;

            @Text(required = false)
            public String link;

            @Override
            public String toString() {
                return "Link{" +
                        "href='" + href + ''' +
                        ", rel='" + rel + ''' +
                        ", contentType='" + contentType + ''' +
                        ", link='" + link + ''' +
                        '}';
            }
        }

        @Root(name = "image")
        public static class ImageBean {
            @Element
            public String title;
            @Element
            public String url;
            @Element
            public String width;
            @Element
            public String height;
            @Element
            public String description;
            @Element
            public String link;

            @Override
            public String toString() {
                return "ImageBean{" +
                        "title='" + title + ''' +
                        ", url='" + url + ''' +
                        ", width='" + width + ''' +
                        ", height='" + height + ''' +
                        ", description='" + description + ''' +
                        ", link='" + link + ''' +
                        '}';
            }
        }

        @Root(name = "meta")
        public static class MetaBean {
            @Attribute(required = false)
            public String name;

            @Attribute(required = false)
            public String content;

            @Override
            public String toString() {
                return "MetaBean{" +
                        "name='" + name + ''' +
                        ", content='" + content + ''' +
                        '}';
            }
        }
    }
}

2) TestSimpleXmlConvert method

package com.example.xyzreader.manager;

import android.util.Log;

import com.example.xyzreader.data.bean.Rss;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.convert.AnnotationStrategy;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.strategy.Strategy;

/**
 * @author zmingchun
 * @version 1.0.0 (2017/3/20)
 */
public class TestSimpleXmlConvert {
    /**
     * test convert by simple-xml-2.7.1
     */
    public static void main(String[] args) {
        Strategy strategy = new AnnotationStrategy();
        Serializer serializer = new Persister(strategy);
        try {
            Rss tRss = serializer.read(Rss.class, "<?xml version="1.0" encoding="UTF-8"?>
" +
                    "<rss  xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
" +
                    "<channel>
" +
                    "    <title>Coding Horror</title>
" +
                    "    <link>http://www.codinghorror.com/blog/</link>
" +
                    "    <description>programming and human factors - Jeff Atwood</description>
" +
                    "    <language>en-us</language>
" +
                    "
" +
                    "    <lastBuildDate>Wed, 04 May 2011 20:34:18 -0700</lastBuildDate>
" +
                    "    <pubDate>Wed, 04 May 2011 20:34:18 -0700</pubDate>
" +
                    "    <generator>http://www.typepad.com/</generator>
" +
                    "    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
" +
                    "
" +
                    "    <image>
" +
                    "        <title>Coding Horror</title>
" +
                    "        <url>http://www.codinghorror.com/blog/images/coding-horror-official-logo-small.png</url>
" +
                    "        <width>100</width>
" +
                    "        <height>91</height>
" +
                    "        <description>Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.</description>
" +
                    "        <link>http://www.codinghorror.com/blog/</link>
" +
                    "    </image>
" +
                    "
" +
                    "    <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />   
" +
                    "    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codinghorror" />        
" +
                    "
" +
                    "</channel>
" +
                    " </rss>");
            Log.e(TestSimpleXmlConvert.class.getSimpleName(), "Convert result:"+ tRss.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...