有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SimpleXML反序列化程序

我不熟悉简单的XML,我遇到了一个问题,不确定我做错了什么

我有以下XML:

    <BOOK>
    <CHAPTER>
      <TEXTLINE />
      <TEXTLINE > line of text.... </TEXTLINE>
      <TEXTLINE > line of text... </TEXTLINE>
    </CHAPTER >
    < CHAPTER >
      <TEXTLINE />
      <TEXTLINE > line of text.... </TEXTLINE>
    </CHAPTER >
<BOOK>

我的java课程:

   @Root
    public static class Book {

        @ElementList(entry = "CHAPTER", inline = true) public ArrayList<ReceiptElement> chapter;

         public ReceiptElement getChapterLines() {
            if (this. chapter != null && this. chapter.size() > 0)
                return  chapter.get(0);
            else
                return null;
        }

    }

    @Root
    public static class Chapter {

        @ElementList(entry = "TEXTLINE", inline = true) public ArrayList<TextLine> lines;

       public ArrayList<TextLine> getLines() {
            return this.lines;
        }


     }

    public static class TextLine {

        private String textline;

        public  TextLine(@ElementMap(entry = "TEXTLINE") String text) {
            this.textline = text;
        }

        public String getTextline() { return  this.textline; }
    }

我得到的例外情况是:

org.simpleframework.xml.core.ConstructorException: Parameter '' does not have a match in class TextLine

我试过几种变体,但都没有成功

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    在解析之前,首先检查XML是否有效。您可以在这里签入在线XML验证程序:http://www.xmlvalidation.com/
    尝试删除<CHAPTER>标记之间的空格,并结束<BOOK>

    我认为这个异常org.simpleframework.xml.core.ConstructorException: Parameter '' does not have a match in class TextLine是因为您没有将@Root添加到TextLine类中

     <BOOK>
       <CHAPTER>
         <TEXTLINE/>
         <TEXTLINE> line of text.... </TEXTLINE>
         <TEXTLINE> line of text... </TEXTLINE>
       </CHAPTER>
       <CHAPTER>
         <TEXTLINE/>
         <TEXTLINE> line of text.... </TEXTLINE>
       </CHAPTER>
     </BOOK>
    

    这本书。java

    @Root
        public class Book {
            @ElementList(entry = "CHAPTER", inline = true) public ArrayList<Chapter> chapter;
    
             public Chapter getChapterLines() {
                if (this. chapter != null && this. chapter.size() > 0)
                    return  chapter.get(0);
                else
                    return null;
            }
        }
    

    第章。java

    @Root
    public class Chapter {
    
        @ElementList(entry = "TEXTLINE", inline = true) public ArrayList<TextLine> lines;
    
       public ArrayList<TextLine> getLines() {
            return this.lines;
        }
     }
    

    文本行。java

     @Root
        public class TextLine {
    
            private String textline;
    
            public  TextLine(@ElementMap(entry = "TEXTLINE") String text) {
                this.textline = text;
            }
            public String getTextline() { return  this.textline; }
        }