如何将xml转换为java

时间:2016-02-22 15:47:55

标签: java xml unmarshalling

我正在尝试从xml文件转换为java,我没有经验。有人可以帮助我。 这是我的下册课。

KVM

现在我想将xml文件转换为java对象xml文件,我将在下面附上。

BOCHS

到目前为止,我试图通过在我的man类

中创建方法来转换和读取xml文件
import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement(name= "book")
public class book {

private String ID;
private String title;
private String author;
private String genre;
private String price;
private String publicationDate;
private String discription;



public book(String a, String b, String c, String d, String e, String f, String g ){
    ID = a;
    title = b;
    author = c;
    genre =  d;
    price = e;
    publicationDate = f;
    discription = g;
}
@XmlElement
public String getID() {
    return ID;
}

public void setID(String iD) {
    ID = iD;
}
@XmlElement
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
@XmlElement
public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}
@XmlElement
public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}
@XmlElement
public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}
@XmlElement
public String getPublicationDate() {
    return publicationDate;
}

public void setPublicationDate(String publicationDate) {
    this.publicationDate = publicationDate;
}
@XmlElement
public String getDiscription() {
    return discription;
}

public void setDiscription(String discription) {
    this.discription = discription;
   }

}

1 个答案:

答案 0 :(得分:2)

您的代码存在几个问题。

  1. @XMLAttribute是id的正确注释,符合您的xml ID 是书的属性而非作为作者或描述的元素。
  2. @XMLElement应该在setter方法上而不是在getter方法
  3. 不确定为什么你在book.java中有一个构造函数,也删除它。
  4. 什么是目录,没有必要。如果您有任何问题,我已附上工作代码,请告诉我。
  5. ************** TEST.java ************     import java.io.File;

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Unmarshaller;
    
    public class Test {
    
        public static void unma() throws JAXBException {
             JAXBContext jc = JAXBContext.newInstance(Books.class);
             Unmarshaller um = jc.createUnmarshaller();
             Books b = (Books) um.unmarshal(new File("c:/tester/books.xml"));
    
             for (int i =0;i<b.getBooks().size();i++) {
             Book bb =   b.getBooks().get(i);
             System.out.println(bb.getAuthor());
             System.out.println(bb.getTitle());
             System.out.println(bb.getDescription());
             System.out.println(bb.getGenre());
             System.out.println(bb.getPrice());
             System.out.println(bb.getDate());
    
             }
    
    
    
        }
    
        public static void main(String[] args) throws JAXBException {
    
                unma();
        }
    
     }    
    

    ********** Books.java **********

    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="Books")
    public class Books {
    
        List<Book> books;
    
        public List<Book> getBooks() {
            return books;
        }
    
        @XmlElement( name = "Book" ) 
        public void setBooks( List<Book> books ) 
        { 
    
            this.books = books; 
    
        } 
    
    }
    

    ************* Book.java ********

    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    @XmlType( propOrder = { "author", "description","title", "genre","price", "date"} )
    @XmlRootElement( name = "Book" )
    public class Book
    {
        String author;
        String description;
        String title;
        String genre;
        String price;
        String date;
    
        public String getAuthor() {
            return author;
        }
    
        @XmlElement( name = "author" )
        public void setAuthor( String author )
        {
            this.author = author;
        }
    
    
    
        public String getDescription() {
            return description;
        }
    
        @XmlElement( name = "description" )
        public void setDescription( String description )
        {
            this.description = description;
        }
    
        public String getTitle() {
            return title;
        }
    
        @XmlElement( name = "title" )
        public void setTitle( String title )
        {
            this.title = title;
        }
    
        public String getGenre() {
            return genre;
        }
    
        @XmlElement( name = "genre" )
        public void setGenre( String genre )
        {
            this.genre = genre;
        }
    
        public String getPrice() {
            return price;
        }
    
        @XmlElement( name = "price" )
        public void setPrice( String price )
        {
            this.price = price;
        }
    
        public String getDate() {
            return date;
        }
    
        @XmlElement( name = "date" )
        public void setDate( String date )
        {
            this.date = date;
        }
    
    }
    

    ** xml for book ****

      <?xml version="1.0"?>
      <Books>
        <Book id="1">
           <author>Isaac Asimov</author>
           <title>Foundation</title>
           <genre>Science Ficition</genre>
           <price>164</price>
           <date>1951-08-21</date>
           <description>Excellent.</description>
        </Book>
        <Book id="2">
           <author>Isaac Asimov</author>
           <title>Foundation and Empire</title>
           <genre>Science Fiction</genre>
           <price>79</price>
           <date>1952-10-12</date>
           <description>Good.</description>
         </Book>
    
     </Books>