【问题标题】:How to convert xml to java如何将xml转换为java
【发布时间】:2016-06-04 03:10:20
【问题描述】:

我正在尝试从 xml 文件转换为 java,我对此没有经验。所以有人可以帮我解决这个问题。 这是我下面的书课。

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;
   }

}

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

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

到目前为止,我尝试通过在我的 man 类中创建下面的方法来转换和读取 xml 文件

enter code here
import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void unma() throws JAXBException {
         JAXBContext jc = JAXBContext.newInstance(book.class);
         Unmarshaller um = jc.createUnmarshaller();
         book b = (book) um.unmarshal(new File("src/Dv600/books.xml"));
         System.out.println("information");
         System.out.println("id" + b.getID());
         System.out.println("Author" + b.getAuthor());
         System.out.println("description" + b.getDiscription());

    }

    public static void main(String[] args) throws JAXBException {

            unma();
    }

 }

【问题讨论】:

  • 它不工作它给了我错误。但我认为我没有正确转换它我想将此 xml 文件转换为 java 对象。
  • 请发布错误。
  • 您需要发布异常,以便我们可以看到出了什么问题。但我的第一个猜测是它在这个语句中找不到你的 XML 文件new File("src/Dv600/books.xml")

标签: java xml unmarshalling


【解决方案1】:

您的代码存在几个问题。

  1. @XMLAttribute 是 id 的正确注释,根据您的 xml ID 是 book 的属性,而不是作为作者或描述的元素。
  2. @XMLElement 应该在 setter 方法上而不是在 getter 方法上
  3. 不知道为什么 book.java 中有构造函数,请将其删除。
  4. 什么是目录,没必要。我已附上工作代码,如果您有任何问题,请告诉我。

****************** TEST.java************ 导入 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

  <?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>

【讨论】:

  • 尝试格式化答案,如果任何有经验的人为我这样做,将不胜感激。
  • 非常感谢@peeskillet。
  • 非常感谢您的友好回复。我试过你的代码,但不幸的是它不起作用我认为它的问题是我的文件或其他东西,但是我复制了你的 xml 文件并运行它给了我这个错误,线程“main”中的异常 javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“书”)。预期的元素是 com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726) 处的 com.sun.xml.internal.bind.v2.runtime。 unmarshaller.Loader.reportError(Loader.java:247) 在
  • 您是否对我提供的 xml 进行了任何更改。你添加了额外的书。只要保持一本书的结构,看看它是否有效。与此同时,我会在我身边尝试
  • 不,我根本没有改变任何东西,我首先尝试让它工作,然后我会进一步检查,但它不起作用
猜你喜欢
  • 1970-01-01
  • 2018-07-19
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
相关资源
最近更新 更多