标签:import input type filters code tno ext catch mon
package com.yyl.text; import java.io.FileInputStream; import java.util.ArrayList; import org.junit.Test; import org.nuxeo.common.xmap.XMap; import com.yin.vo.Book; import com.yin.vo.Books; public class XmapTest { /** * 读取XML文件内容 * @throws Exception */ @Test public void createXML() throws Exception{ XMap xmp = new XMap(); //解析xml 将xml内容转换成javaBean xmp.register(Books.class); FileInputStream in = new FileInputStream("D:\\WORK\\workspace\\MyProject\\src\\com\\yin\\book.xml"); Object[] result = (Object[]) xmp.loadAll(in); Books bookes=(Books)result[0]; System.out.println(bookes.getBook().get(0).getTitle()); } /** * 将对象转成xml字符串 */ //@Test public void readXML(){ Books books=new Books(); Book book=new Book(); ArrayList<String> sj=new ArrayList<String>(); sj.add("java程序设计啦啦集合list"); sj.add("j"); book.setNo("1"); book.setPrice("15块钱"); book.setTitle("java程序设计"); book.setSubjects(sj); books.addBook(book); Book book2=new Book(); book2.setNo("2"); book2.setPrice("25块钱"); book2.setTitle("c程序设计傻逼"); ArrayList<String> li=new ArrayList<String>(); li.add("c程序设计啦啦list"); li.add("C"); book2.setSubjects(li); books.addBook(book2); XMap xmp = new XMap(); //将javaBean转换成xml文件 xmp.register(Books.class); ArrayList<String> filters=new ArrayList<String>(); try { System.out.println(xmp.asXmlString(books, "UTF-8", filters, true)); } catch (Exception e) { e.printStackTrace(); } } }
books.java
package com.yyl.vo; import java.util.ArrayList; import java.util.List; import org.nuxeo.common.xmap.annotation.XNodeList; import org.nuxeo.common.xmap.annotation.XObject; @XObject(value = "books") public class Books { @XNodeList(value = "book", type = ArrayList.class, componentType = Book.class) private List< Book> book; public List< Book> getBook() { return book; } public void setBook(List< Book> book) { this.book = book; } public void addBook(Book b){ if(book==null){ book=new ArrayList<Book>(); } book.add(b); } }
book.java
package com.yyl.vo; import java.util.ArrayList; import java.util.List; import org.nuxeo.common.xmap.annotation.XNode; import org.nuxeo.common.xmap.annotation.XNodeList; import org.nuxeo.common.xmap.annotation.XObject; @XObject public class Book { @XNode("title") private String title; @XNode("@no") private String no; @XNodeList(value = "subject", type = ArrayList.class, componentType = String.class) private List< String> subjects; @XNode("price") private String price; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List< String> getSubjects() { return subjects; } public void setSubjects(List< String> subjects) { this.subjects = subjects; } public String getPrice() { return price; } public void setPrice(String t) { this.price =t; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } }
book.xml
<?xml version="1.0" encoding="UTF-8"?> <books> <book no="1"> <title>java程序设计</title> <subject>java</subject> <subject>j</subject> <price>10块钱</price> </book> <book no="2"> <title>c++是傻逼</title> <subject>C++</subject> <subject>c</subject> <price>10块钱</price> </book> </books>
标签:import input type filters code tno ext catch mon
原文地址:https://www.cnblogs.com/yinyl/p/11805049.html