xml与java对象映射

JAXB提供了XML到java对象(类)之间的相互映射(转换),我们可以利用JAXB来消除繁琐的XML解析工作。

下面是引用oracle网站中有关jaxb的一句话。

 

Now developers have another Java API at their disposal that can make it easier to access XML documents: Java Architecture for XML Binding (JAXB). A Reference Implementation of the API is now available in the Java Web Services Developer Pack V 1.1.

 

JAXB包括两部分:

1.编译器程序:负责根据XML Schema(xsd文件)编译生成对应java类文件

2.JAXB API:程序中调用这些api,再结合1中的类达到映射的目的

 

对于JAXB的安装及编译器部分的使用请参见:http://maimode.iteye.com/admin/blogs/1329929

 

本篇将主要针对api部分给出代码示例,并在后文中给出文档校验的方法。

 

无论是将xml映射为java对象还是将java对象映射为xml,都首先要使用JAXB的编译器将XSD文件编译成java类文件,然后在项目中导入这些类。

 

需要引用一下类:

 

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

 

 

A. 将XML映射为java对象

// 建立上下文 amos.note为编译器生成的类文件的包名称
JAXBContext jaxbContext = JAXBContext.newInstance("amos.note");
// 建立unmashaller对象
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
// 将xml文件映射为java对象
Note note = (Note) unmarshaller.unmarshal(new File("e:/t/t.xml"));
// TODO ....

 以上代码片段就是将t.xml文件中的根Note节点映射为了Note对象,也可以将其他形式的xml进行映射。

 

 

B. 将java对象映射为XML

// 准备要映射的对象 amos.note.ObjectFactory是编译器生成的类
amos.note.ObjectFactory objFactory = new amos.note.ObjectFactory();
Note note = objFactory.createNote();
//上面的note对象也可是A部分中的那个note对象,这样A、B两个步骤和在一起就是对xml文件的更新了
// 建立上下文 amos.note为编译器生成的类文件的包名称
JAXBContext jaxbContext = JAXBContext.newInstance("amos.note");
// 建立mashaller对象
Marshaller marshaller = jaxbContext.createMarshaller();
// 将java对象映射为xml文件
marshaller.marshal(note, new FileOutputStream("e:/t/t.xml"));

 以上代码片段就是将Note对象映射为t.xml文件中的根Note节点,也可以映射成为其他形式的对象。

 

 

基于XSDXML文档校验

 

参考:http://www.ibm.com/developerworks/cn/xml/x-javaxmlvalidapi.html

 

Java 5 引入了 javax.xml.validation 包,提供了独立于模式语言的验证服务接口。这个包也可用于 Java 1.3 及更高版本,不过要单独安装 JAXP 1.3。其他产品中,Xerces 2.8 包含了这个库的实现。 

 

javax.xml.validation API 使用三个类来验证文档:SchemaFactorySchema 和 Validator。还大量使用了 TrAX javax.xml.transform.Source 接口来表示 XML 文档。简言之,SchemaFactory 读取模式文档(通常是 XML 文件)并创建 Schema 对象。Schema 创建一个 Validator 对象。最后,Validator 对象验证表示为 Source 的 XML 文档。 

 

改变验证所依据的模式、要验证的文档甚至使用的模式语言都很简单。但无论什么情况,验证都需要经过下列五个步骤:

 

1) 为编写模式所用的语言加载一个模式工厂。

2) 编译源文件中的模式。

3) 用编译后的模式创建一个验证程序。

4) 为需要验证的文档创建 Source 对象。StreamSource 通常最简单。

5) 验证输入的源文档。如果文档无效,validate() 方法将抛出 SAXException。否则什么也不显示。

 

可以反复使用同一个验证程序和同一个模式多次。但是所有类都不是线程安全的或者可重入的。如果用多个线程同时验证,一定要保证每个线程有自己的 Validator 和 Schema 对象。

 

需要导入的包:

 

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
 

 

下面是验证的主要代码:

/**
	 * 根据xsd文件校验一个xml文件是否有效,当无效异常时交由errorHandler处理异常
	 * 
	 * @param xmlFilePath
	 * @param xsdFilePath
	 * @param errorHandler
	 * @return
	 * @throws IOException
	 * @throws SAXException
	 */
	private boolean doValidate(String xmlFilePath, String xsdFilePath,
			ErrorHandler errorHandler) throws IOException, SAXException {
		boolean rt = false;
		// 1. Lookup a factory for the W3C XML Schema language
		SchemaFactory factory = SchemaFactory
				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		// 2. Compile the schema.
		// Here the schema is loaded from a java.io.File, but you could use
		// a java.net.URL or a javax.xml.transform.Source instead.
		File schemaLocation = new File(xsdFilePath);
		Schema schema = factory.newSchema(schemaLocation);

		// 3. Get a validator from the schema.
		Validator validator = schema.newValidator();
		validator.setErrorHandler(errorHandler);

		// 4. Parse the document you want to check.
		Source source = new StreamSource(xmlFilePath);

		// 5. Check the document
		try {
			validator.validate(source);
			rt = true;
		} catch (SAXException ex) {
			rt = false;
		}
		return rt;
	}

需要注意的是,当传入有效的ErrorHandler示例后,执行validator.validate(source)方法时无论是否出现异常都不在抛出SAXException异常。

 

因此,下面的方法包装上面的方法来验证

 

/**
	 * 根据xsd文件校验一个xml文件是否有效
	 * @param xmlFilePath
	 * @param xsdFilePath
	 * @return true-有效 false-无效
	 * @throws IOException
	 * @throws SAXException
	 */
	public boolean validate(String xmlFilePath, String xsdFilePath) throws IOException, SAXException{
		return this.doValidate(xmlFilePath, xsdFilePath, null);
	}

	
	/**
	 * 根据xsd文件校验一个xml文件是否有效,当无效异常时交由errorHandler处理异常
	 * @param xmlFilePath
	 * @param xsdFilePath
	 * @param errorHandler
	 * @throws IOException
	 * @throws SAXException
	 */
	public void validate(String xmlFilePath, String xsdFilePath, ErrorHandler errorHandler) throws IOException, SAXException{
		boolean rt = this.doValidate(xmlFilePath, xsdFilePath, errorHandler);
	}

 

 

其实,验证的这个步骤不必单独占用一些列资源而只做这一件事,可以在上面的映射过程中完成,oracle网站上提供了验证的说明,但是我自己测试发现他说的方法已经废弃了,因此,没有在映射的代码中提到,还有待研究。

 

附件xml.zip是完整的示例程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用DOM、SAX或StAX等库将XML文档解析为Java对象。这里我们介绍一下使用DOM方式将XML内容转换为Java对象。 假设我们有一个XML内容如下: ```xml <person> <name>John Doe</name> <age>30</age> <address> <street>123 Main St</street> <city>Anytown</city> <state>CA</state> <zip>12345</zip> </address> </person> ``` 我们可以使用DOM解析器将其解析为以下Java对象: ```java public class Person { private String name; private int age; private Address address; // getters and setters } public class Address { private String street; private String city; private String state; private String zip; // getters and setters } ``` 我们需要创建一个Document对象,然后使用它获取XML元素并将其映射Java对象。以下是完整的示例代码: ```java import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.*; public class XmlToObject { public static void main(String[] args) throws Exception { String xmlContent = "<person><name>John Doe</name><age>30</age><address><street>123 Main St</street><city>Anytown</city><state>CA</state><zip>12345</zip></address></person>"; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new ByteArrayInputStream(xmlContent.getBytes())); Element root = document.getDocumentElement(); Person person = new Person(); person.setName(getTagValue("name", root)); person.setAge(Integer.parseInt(getTagValue("age", root))); Address address = new Address(); address.setStreet(getTagValue("street", root)); address.setCity(getTagValue("city", root)); address.setState(getTagValue("state", root)); address.setZip(getTagValue("zip", root)); person.setAddress(address); System.out.println(person.getName()); System.out.println(person.getAge()); System.out.println(person.getAddress().getStreet()); System.out.println(person.getAddress().getCity()); System.out.println(person.getAddress().getState()); System.out.println(person.getAddress().getZip()); } private static String getTagValue(String tag, Element element) { NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes(); Node node = nodeList.item(0); return node.getNodeValue(); } } ``` 运行该程序将输出以下内容: ``` John Doe 30 123 Main St Anytown CA 12345 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值