jxb marshaller 代码分享

/**
 * 
 */
package com.project.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.GregorianCalendar;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

/**
 * @author Administrator
 *
 */
public class XmlMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			JAXBContext jcon = JAXBContext.newInstance(Student.class);
			Marshaller marshaller = jcon.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//			marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.org/Student");
			marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "D:/OEPE_IDE/workspace_tttttt/TEST_PROJECT/src/student.xsd");
//			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
			SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
			InputStream is = XmlMain.class.getClassLoader().getResourceAsStream("student.xsd");
			StreamSource ss = new StreamSource(is);
			Schema schema = schemaFactory.newSchema(ss);
			marshaller.setSchema(schema);
			File f = new File("D:/student.xml");
			FileOutputStream fos = new FileOutputStream(f);
			
			DatatypeFactory factory = DatatypeFactory.newInstance();
			XMLGregorianCalendar xCal = factory.newXMLGregorianCalendar(new GregorianCalendar());
			ObjectFactory of = new  ObjectFactory();
			Student s = of.createStudent();
			s.setName("xi");
			s.setBirthday(xCal);
			System.out.println("李四李四");
			
			marshaller.marshal(s, fos);
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}


写上面的代码,有了一点心得,Marshaller.JAXB_FORMATTED_OUTPUT只是是否格式化输出true:格式化输出,false:没有格式化。

如果你的xsd文件没有上传到targetSchema中,那么只能用本地引用,所以使用Marshaller.JAXB_SCHEMA_LOCATION,报文件找不到的错误,而应该改用Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION,写上本地地址。

在创建SchemaFactory对象的时候,需要你传入schemaLanguage,jdk api提供了两种语言,分别是XMLConstants.W3C_XML_SCHEMA_NS_URI ("http://www.w3.org/2001/XMLSchema")、XMLConstants.RELAXNG_NS_URI ("http://relaxng.org/ns/structure/1.0") 。

这个程序不足的地方是中文乱码问题,在解决中,过一段时间修正上去。

猜你喜欢

转载自blog.csdn.net/lztyll123/article/details/8938301