Java读写xml(对复杂数据,使用中间模型)

阅读更多

1、创建.xml,设计.xml结构。

 



	
		
		
		
	
	
		
		
	

 

 2、创建.xsd,可以借助XMLSpy软件,有从.xml生成.xsd功能。

      注意:maxOccurs="unbounded"这句,能表示出Xbizs里可以有很多Xbiz

 


	
		
			
				
				
			
		
	
	
		
			
		
	
	
		
			
		
	
	
		
		
	
	
		
		
		
	


3、生成中间模型,可以使用xjc命令,也可以借助专门编写的"ant打jar包"的.xml。这里我采用后者生成了.jar,结构如下:

 

-test.jaxb.jar
   -test.jaxb
      -ObjectFactory.class
      -Xbiz.class
      -Xbizs.class
      -Xmodel.class
      -Xnode.class
      -Xnodes.class
  

     Xmodel.java内容如下:

 

package test.jaxb;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "xnodes", "xbizs" })
@XmlRootElement(name = "Xmodel")
public class Xmodel {

	@XmlElement(name = "Xnodes", required = true)
	protected Xnodes xnodes;
	@XmlElement(name = "Xbizs", required = true)
	protected Xbizs xbizs;

	public Xnodes getXnodes() {
		return xnodes;
	}

	public void setXnodes(Xnodes value) {
		this.xnodes = value;
	}

	public Xbizs getXbizs() {
		return xbizs;
	}

	public void setXbizs(Xbizs value) {
		this.xbizs = value;
	}
}

 

Xnodes.java内容如下:

 

package test.jaxb;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Xnodes", propOrder = { "xnode" })
public class Xnodes {

	@XmlElement(name = "Xnode", required = true)
	protected List xnode;

	public List getXnode() {
		if (xnode == null) {
			xnode = new ArrayList();
		}
		return this.xnode;
	}
}
 

4、编写本地的对象模型,我这里的对象模型类结构如下:

 

-src
   -object
      -Biz.java
      -Model.java
      -Node.java

 

     Model.java内容如下:

 

package object;

import java.util.ArrayList;
import java.util.List;

public class Model {
	public List nodeList=new ArrayList();
	
	public List bizList=new ArrayList();
}
  

5、读取.xml后,写到另一个.xml。

 

package object;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

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

import test.jaxb.ObjectFactory;
import test.jaxb.Xbiz;
import test.jaxb.Xmodel;
import test.jaxb.Xnode;

public class test {

	public static void main(String[] args) {
		Model model = read();
		if (null != model) {
			write(model);
		}
	}

	public static Model read() {
		try {
			JAXBContext jc = JAXBContext.newInstance("test.jaxb");
			Unmarshaller u = jc.createUnmarshaller();

			Xmodel Xmodel = (Xmodel) u.unmarshal(new File(
					"C:\\read.xml"));

			Model model = new Model();

			Map nodeSet = new HashMap();
			for (Xnode Xnode : Xmodel.getXnodes().getXnode()) {
				Node node = new Node();
				node.id = Xnode.getId();
				node.type = Xnode.getType();

				nodeSet.put(node.id, node);
				model.nodeList.add(node);
			}

			for (Xbiz bizType : Xmodel.getXbizs().getXbiz()) {
				Biz biz = new Biz();
				biz.id = bizType.getId();
				biz.snode = nodeSet.get(bizType.getSnode());
				biz.tnode = nodeSet.get(bizType.getTnode());

				model.bizList.add(biz);
			}

			System.out.println(model.nodeList.size());
			for (Node node : model.nodeList) {
				System.out.println(node.id + " " + node.type);
			}

			System.out.println();

			System.out.println(model.bizList.size());
			for (Biz biz : model.bizList) {
				System.out.println(biz.id + " " + biz.snode.id + " "
						+ biz.snode.type + " " + biz.tnode.id + " "
						+ biz.tnode.type);
			}

			return model;
		} catch (JAXBException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void write(Model model) {
		try {
			JAXBContext jc = JAXBContext.newInstance("test.jaxb");
			Marshaller m = jc.createMarshaller();

			ObjectFactory factory = new ObjectFactory();
			Xmodel Xmodel = factory.createXmodel();

			for (Node node : model.nodeList) {
				Xnode Xnode = factory.createXnode();
				Xnode.setId(node.id);
				Xnode.setType(node.type);

				Xmodel.getXnodes().getXnode().add(Xnode);
			}

			Xmodel.setXbizs(factory.createXbizs());
			for (Biz biz : model.bizList) {
				Xbiz bizType = factory.createXbiz();
				bizType.setId(biz.id);
				bizType.setSnode(biz.snode.id);
				bizType.setTnode(biz.tnode.id);

				Xmodel.getXbizs().getXbiz().add(bizType);
			}

			m.marshal(Xmodel, new File(
					"C:\\write.xml"));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}
打印结果:
3
n1 node2
n2 node3
n3 node4

2
b1 n1 node2 n2 node3
b2 n3 node4 n1 node2

你可能感兴趣的:(Java读写xml(对复杂数据,使用中间模型))