37461

How parse xml using jaxb

I am new in JAXB, I want to read XML file to Java object, which is in the following format:

<payTypeList> 
    <payType>G</payType> 
    <payType>H</payType> 
    <payType>Y</payType> 
 </payTypeList>


Please help me how to read this type of XML.

Answer1:

<strong>This is your scenario</strong>

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.XmlRootElement; @XmlRootElement(name="payTypeList") @XmlAccessorType(XmlAccessType.FIELD) public class PayTypeList { @XmlElement private List<String> payType; public List<String> getPayType() { return payType; } public void setPayType(List<String> payType) { this.payType = payType; } }

<strong>Method to use</strong>

public static void main(String[] args) throws JAXBException { final JAXBContext context = JAXBContext.newInstance(PayTypeList.class); final Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); final PayTypeList paymentType = new PayTypeList(); List<String> paymentTypes = new ArrayList<String>(); paymentTypes.add("one"); paymentTypes.add("two"); paymentTypes.add("three"); paymentType.setPayType(paymentTypes); m.marshal(paymentType, System.out); }

<strong>Output.xml</strong>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <payTypeList> <payType>one</payType> <payType>two</payType> <payType>three</payType> </payTypeList>

Answer2:

For this use case you will have a single class with a List<String> property. The only annotations you may need to use are @XmlRootElement and @XmlElement to map the List property to the payType element.

<strong>For More Information</strong>

You can find more information about using these annotations on my blog:

Recommend

  • XML unable to create JAXBContext on marshal [duplicate]
  • How to provide JACKSON with the namespace-mapper in the code?
  • Jetty + Jersey + Jackson, different behavior in Eclipse (Success) vs command line (Error Unsupported
  • Line number of individual XML element while unmarshalling using jaxb
  • Jaxb : Append field of Request XML without modfying JAXB java class
  • How parse xml using jaxb
  • Retrieve value from CDATA
  • How to discard only the last element of CKEditor undo stack?
  • Spring: No transaction manager has been configured
  • Spring Cloud Microservice Architecture Confusion
  • Linq Objects Group By & Sum
  • Retrieving value from sql ExecuteScalar()
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • output of program is not same as passed argument
  • Javascript + PHP Encryption with pidCrypt
  • To display the title for the current loaction in map in iphone
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • KeystoneJS: Relationships in Admin UI not updating
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • unknown Exception android
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?