JAXB:XML to JavaBean

本文介绍了一种使用XML作为配置文件并通过Java进行解析的方法。具体展示了如何定义XML Schema来约束配置文件的内容,并通过Java的JAXB技术实现XML到Java对象的自动转换。此外,还提供了具体的Java代码示例,包括配置类和子元素类的设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Config.xml

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.example.org/config" 
		xmlns="http://www.example.org/config" 
		elementFormDefault="qualified">
		
	<xsd:element name="config">
    	<xsd:complexType>
    		<xsd:sequence>
    			<xsd:element name="case" maxOccurs="unbounded" minOccurs="0">
                    <xsd:complexType>
                    	<xsd:attribute name="name" type="xsd:string" use="required"></xsd:attribute>
                    </xsd:complexType>
    			</xsd:element>
    		</xsd:sequence>
    	</xsd:complexType>
     </xsd:element>

</xsd:schema>

<config xmlns="http://www.example.org/config"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
			xsi:schemaLocation="http://www.example.org/config 
								http://www.example.org/config/schema/config.xsd">
			
	<case name="case1">
		case1 content
	</case>
	
	<case name="case2">
		case2 content
	</case>
	
</config>

主体对应配置类

package com.test;

import java.io.InputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="config", namespace=Config.NS)
public class Config {
	public final static String NS = "http://www.example.org/config";
	
	private List<Case> caseList;

	@XmlElement(name="case", namespace=Config.NS)
	public List<Case> getCaseList() {
		return caseList;
	}

	public void setCaseList(List<Case> caseList) {
		this.caseList = caseList;
	}
	
	public static Config loadConfig(InputStream inStream)  {   
        try {  
            JAXBContext jaxbContext = JAXBContext.newInstance(Config.class);  
            Unmarshaller um = jaxbContext.createUnmarshaller();
            return Config.class.cast(um.unmarshal(inStream));  
        } catch (JAXBException e) {  
            throw new RuntimeException(e); 
        } 
	}
	
	public static void main(String[] args) {
		Config config = Config.loadConfig(Config.class.getResourceAsStream("config.xml"));
		for(Case curCase : config.getCaseList()) {
			System.out.println(curCase.getName() + ": " + curCase.getValue());
		}
	}
}

子元素对应类

package com.test;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

public class Case {
	private String name;
	private String value;
	
	@XmlAttribute(name="name")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@XmlValue
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value.trim();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值