首页 > 编程知识 正文

jaxb修改节点名称,jaxb设置类的节点

时间:2023-05-06 03:06:38 阅读:147752 作者:3354

http://www.Sina.com/JavaXML绑定JAXB教程请参见-ultimate指南

什么是JAXB? JXB表示用于XML绑定的Java体系结构。 用于将XML转换为java对象,并将java对象转换为XML。 JXB定义了用于在XML文档中读写Java对象的API。 与SAX和DOM不同,您不需要了解XML分析技术。

可以使用JAXB执行以下两项操作

组:将Java对象转换为XML组:在“将XML转换为Java对象”教程中,您将创建封装和封装的Java程序。

对于组:

对于取消分组:

Java程序:使用JAXB提供的注释和API将Java对象转换为XML,反之亦然,非常简单。

1 .用于XML之间转换的Java对象

在src-org.ARP it.javapostsforlearning.JAXB中创建Country.java

package org.ARP it.javapostsforlearning.JAXB; import java.util.ArrayList; import javax.XML.bind.annotation.XML element; import javax.XML.bind.annotation.xmlelementwrapper; import javax.XML.bind.annotation.XML rootelement; import javax.XML.bind.annotation.xmltype; //belowannotationdefinesrootelementofxmlfile @ XML rootelement//youcandefineorderinwhichelementswillbecreatedinxmlfile/optional @ xmltype (proporder={ ' country name ',' countryPopulation ',' listOfStates'} ) public class country { private 权限阵列列表状态列表状态; 公共计数{ }公共字符串计数名称{ }返回计数名称; } @ xmlelementpublicvoidsetcountryname (string country name ) { this.countryName=countryName; } publicdoublegetcountrypopulation () { return countryPopulation; } @ xmlelementpublicvoidsetcountrypopulation (doublecountrypopulation ) this.country population=country population; } publicarrayliststategetlistofstates () ) { return listOfStates; //xmlelementwrappergeneratesawrapperelementaroundxmlrepresentation @ xmlelementwrapper (name=' state list ' )/xmlelementsetsthenameoftheentitiesincollection @ XML element (name=' state ' ) publicvoidsetlistofstates )阵列

@xmltype(proporder={ '属性列表顺序' } ) :用于定义XML文件中元素的顺序。 这是可选的。

@ XML element :用于定义XML文件中的元素,并设置实体名称。 @xmlelementwrapper(name=“命名包装器”)在XML表示周围生成包装器元素。 例如,在上面的示例中,它是在stateList的每个state元素周围生成的

2 .状态库package org.ARP it.javapostsforlearning.JAXB; import javax.XML.bind.annotation.XML rootelement; //belowstatementmeansthatclass ' country.Java ' is the root-El

ement of our example@XmlRootElement(namespace = 'org.arpit.javapostsforlearning.jaxb.Country')public class State { private String stateName; long statePopulation; public State() { } public State(String stateName, long statePopulation) { super(); this.stateName = stateName; this.statePopulation = statePopulation; } public String getStateName() { return stateName; } public void setStateName(String stateName) { this.stateName = stateName; } public long getStatePopulation() { return statePopulation; } public void setStatePopulation(long statePopulation) { this.statePopulation = statePopulation; }} 3.JAXBJavaToXml.java package org.arpit.javapostsforlearning.jaxb;import java.io.File;import java.util.ArrayList;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;public class JAXBJavaToXml { public static void main(String[] args) { // creating country object Country countryIndia=new Country(); countryIndia.setCountryName('India'); countryIndia.setCountryPopulation(5000000); // Creating listOfStates ArrayList<state> stateList=new ArrayList<state>(); State mpState=new State('Madhya Pradesh',1000000); stateList.add(mpState); State maharastraState=new State('Maharastra',2000000); stateList.add(maharastraState); countryIndia.setListOfStates(stateList); try { // create JAXB context and initializing Marshaller JAXBContext jaxbContext = JAXBContext.newInstance(Country.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // for getting nice formatted output jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //specify the location and name of xml file to be created File XMLfile = new File('C:\arpit\CountryRecord.xml'); // Writing to XML file jaxbMarshaller.marshal(countryIndia, XMLfile); // Writing to console jaxbMarshaller.marshal(countryIndia, System.out); } catch (JAXBException e) { // some exception occured e.printStackTrace(); } }}

运行以上程序后,将得到以下输出

控制台输出: <?xml version='1.0' encoding='UTF-8' standalone='yes'?><country xmlns:ns2='org.arpit.javapostsforlearning.jaxb.Country'> <countryName>India</countryName> <countryPopulation>5000000.0</countryPopulation> <stateList> <state> <stateName>Madhya Pradesh</stateName> <statePopulation>1000000</statePopulation> </state> <state> <stateName>Maharastra</stateName> <statePopulation>2000000</statePopulation> </state> </stateList></country>

现在,我们将阅读上面生成的XML并从中获取国家对象。

4.JAXBXMLToJava.java package org.arpit.javapostsforlearning.jaxb;import java.io.File;import java.util.ArrayList;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;public class JAXBXMLToJava { public static void main(String[] args) { try { // create JAXB context and initializing Marshaller JAXBContext jaxbContext = JAXBContext.newInstance(Country.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); // specify the location and name of xml file to be read File XMLfile = new File('C:\arpit\CountryRecord.xml'); // this will create Java object - country from the XML file Country countryIndia = (Country) jaxbUnmarshaller.unmarshal(XMLfile); System.out.println('Country Name: '+countryIndia.getCountryName()); System.out.println('Country Population: '+countryIndia.getCountryPopulation()); ArrayList<state> listOfStates=countryIndia.getListOfStates(); int i=0; for(State state:listOfStates) { i++; System.out.println('State:'+i+' '+state.getStateName()); } } catch (JAXBException e) { // some exception occured e.printStackTrace(); } }}

运行以上程序后,将得到以下输出:

控制台输出: Country Name: IndiaCountry Population: 5000000.0State:1 Madhya PradeshState:2 Maharastra JAXB的优点: 它比DOM或SAX解析器简单易用 我们可以将XML文件编组到其他数据目标,例如inputStream,URL,DOM节点。 我们可以从其他数据目标中解组XML文件。 我们不需要了解XML解析技术。 我们不需要总是访问树结构中的XML。 JAXB的缺点: JAXB是高层API,因此与SAX或DOM相比,它对解析的控制更少。 它有一些开销的任务,因此它比SAX慢。 源代码:

下载

参考: JAXB教程–从我们的JCG合作伙伴 Arpit Mandliya 入门 , 了解有关初学者博客的Java框架和设计模式 。

翻译自: https://www.javacodegeeks.com/2013/02/jaxb-tutorial-getting-started.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。