java读取XML文件,及封装XML字符串

Posted 一路向前

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java读取XML文件,及封装XML字符串相关的知识,希望对你有一定的参考价值。

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>

 

以上是关于java读取XML文件,及封装XML字符串的主要内容,如果未能解决你的问题,请参考以下文章

java中XML操作:xml与string互转读取XML文档节点及对XML节点增删改查

Java web——xml文件读取的解析方式(DOM和SAX)

java怎么在xml文件中保存和读取字符串数组

读取在java中存储为字符串的xml [重复]

jar包里面的代码如何读取jar包中的配置文件?

Java文件操作——XML文件的读取