如何将XML转换为Java

亚历克斯

我正在尝试从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();
    }

 }
用户名

您的代码有几个问题。

  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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将Scala Scales XML转换为DOM / SAX

来自分类Dev

如何将XML转换为XSL

来自分类Dev

如何将rdf xml转换为n-triples?

来自分类Dev

对于超过3.0的Unicode版本,如何将Java字符串转换为xml实体?

来自分类Dev

如何将XML数据转换为SQL Server表

来自分类Dev

如何将jSON转换为XML

来自分类Dev

如何将StringWriter中的值转换为XML格式?

来自分类Dev

如何将XLSB中的.bin转换为XML?

来自分类Dev

如何将XML元素转换为文本

来自分类Dev

如何将Spring Integration XML转换为Java DSL以获得errorChannel

来自分类Dev

如何将字节数组转换为xml?

来自分类Dev

如何将CSV文件转换为XML?

来自分类Dev

如何将XML文件转换为Pandas数据框

来自分类Dev

如何将XML转换为JSON

来自分类Dev

如何将fetch()转换为XML HTTP请求?

来自分类Dev

如何将XML转换为CSV(C#)

来自分类Dev

如何将xml对象转换为python对象?

来自分类Dev

如何将CSV文件转换为XML?

来自分类Dev

如何将txt文件转换为xml?

来自分类Dev

如何将XML Symfony服务文件转换为Yaml

来自分类Dev

如何将Xml转换为字符串数组

来自分类Dev

如何将XML转换为C ++字符串

来自分类Dev

如何将XML转换为OCI-Lob对象?

来自分类Dev

如何将vsdx文件转换为XML文件

来自分类Dev

如何将嵌套的XML元素转换为平面XML

来自分类Dev

如何将String xml转换为XML节点XSLT

来自分类Dev

如何将Android APK转换为Java和XML文件?

来自分类Dev

我如何将 web.xml 中的 login-config 转换为 java 类?

来自分类Dev

如何将嵌套的 XML 复杂元素转换为 Java 中的 Saxon.Sequence?