unmarshal an xml file using JAXB












0















[UPDATE]
i am trying to use jaxb to store xml element into an arraylist.
i saw many tutorials for storing below xml format into an object or arraylist



<country>
<name>nepal</name>
<zip>123</zip>
</country>


and i tried with different kind of xml file like below and that did not work out



<TransactionList>
<Transaction type="D" amount="61" narration="Electricity bill" />
<Transaction type="D" amount="32" narration="Social security payment" />
<Transaction type="D" amount="33" narration="Payment sent to x" />
<Transaction type="C" amount="111" narration="Salary" />
<Transaction type="D" amount="233" narration="Car rental" />




i tried like this below code



try {

File file = new File("C:\Users\anon\Desktop\Transaction_Data.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Transaction.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Transaction transaction = (Transaction) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);


} catch (JAXBException e) {
e.printStackTrace();
}


my pojo class(Transaction.java)



@XmlRootElement(name="TransactionList")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {

@XmlElement(name="type")
private String type;

@XmlElement(name="amount")
private BigDecimal amount;

@XmlElement(name="narration")
private String narration;

@XmlElement(name = "Transaction")
private List<Transaction> transaction= null;

public List<Transaction> getTransaction() {
return transaction;
}

public void setTransaction(List<Transaction> transaction) {
this.transaction = transaction;
}


i am getting this as output now



com.progressoft.induction.tp.models.Transaction@0


when i try to do this



transaction.getAmount(); 


then i get null










share|improve this question

























  • Try @XmlAttribute instead of @XmlElement

    – Thomas Fritsch
    Nov 20 '18 at 11:45











  • no luck, i am getting null output :(

    – Jasbin Karki
    Nov 20 '18 at 12:20
















0















[UPDATE]
i am trying to use jaxb to store xml element into an arraylist.
i saw many tutorials for storing below xml format into an object or arraylist



<country>
<name>nepal</name>
<zip>123</zip>
</country>


and i tried with different kind of xml file like below and that did not work out



<TransactionList>
<Transaction type="D" amount="61" narration="Electricity bill" />
<Transaction type="D" amount="32" narration="Social security payment" />
<Transaction type="D" amount="33" narration="Payment sent to x" />
<Transaction type="C" amount="111" narration="Salary" />
<Transaction type="D" amount="233" narration="Car rental" />




i tried like this below code



try {

File file = new File("C:\Users\anon\Desktop\Transaction_Data.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Transaction.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Transaction transaction = (Transaction) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);


} catch (JAXBException e) {
e.printStackTrace();
}


my pojo class(Transaction.java)



@XmlRootElement(name="TransactionList")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {

@XmlElement(name="type")
private String type;

@XmlElement(name="amount")
private BigDecimal amount;

@XmlElement(name="narration")
private String narration;

@XmlElement(name = "Transaction")
private List<Transaction> transaction= null;

public List<Transaction> getTransaction() {
return transaction;
}

public void setTransaction(List<Transaction> transaction) {
this.transaction = transaction;
}


i am getting this as output now



com.progressoft.induction.tp.models.Transaction@0


when i try to do this



transaction.getAmount(); 


then i get null










share|improve this question

























  • Try @XmlAttribute instead of @XmlElement

    – Thomas Fritsch
    Nov 20 '18 at 11:45











  • no luck, i am getting null output :(

    – Jasbin Karki
    Nov 20 '18 at 12:20














0












0








0








[UPDATE]
i am trying to use jaxb to store xml element into an arraylist.
i saw many tutorials for storing below xml format into an object or arraylist



<country>
<name>nepal</name>
<zip>123</zip>
</country>


and i tried with different kind of xml file like below and that did not work out



<TransactionList>
<Transaction type="D" amount="61" narration="Electricity bill" />
<Transaction type="D" amount="32" narration="Social security payment" />
<Transaction type="D" amount="33" narration="Payment sent to x" />
<Transaction type="C" amount="111" narration="Salary" />
<Transaction type="D" amount="233" narration="Car rental" />




i tried like this below code



try {

File file = new File("C:\Users\anon\Desktop\Transaction_Data.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Transaction.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Transaction transaction = (Transaction) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);


} catch (JAXBException e) {
e.printStackTrace();
}


my pojo class(Transaction.java)



@XmlRootElement(name="TransactionList")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {

@XmlElement(name="type")
private String type;

@XmlElement(name="amount")
private BigDecimal amount;

@XmlElement(name="narration")
private String narration;

@XmlElement(name = "Transaction")
private List<Transaction> transaction= null;

public List<Transaction> getTransaction() {
return transaction;
}

public void setTransaction(List<Transaction> transaction) {
this.transaction = transaction;
}


i am getting this as output now



com.progressoft.induction.tp.models.Transaction@0


when i try to do this



transaction.getAmount(); 


then i get null










share|improve this question
















[UPDATE]
i am trying to use jaxb to store xml element into an arraylist.
i saw many tutorials for storing below xml format into an object or arraylist



<country>
<name>nepal</name>
<zip>123</zip>
</country>


and i tried with different kind of xml file like below and that did not work out



<TransactionList>
<Transaction type="D" amount="61" narration="Electricity bill" />
<Transaction type="D" amount="32" narration="Social security payment" />
<Transaction type="D" amount="33" narration="Payment sent to x" />
<Transaction type="C" amount="111" narration="Salary" />
<Transaction type="D" amount="233" narration="Car rental" />




i tried like this below code



try {

File file = new File("C:\Users\anon\Desktop\Transaction_Data.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Transaction.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Transaction transaction = (Transaction) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);


} catch (JAXBException e) {
e.printStackTrace();
}


my pojo class(Transaction.java)



@XmlRootElement(name="TransactionList")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {

@XmlElement(name="type")
private String type;

@XmlElement(name="amount")
private BigDecimal amount;

@XmlElement(name="narration")
private String narration;

@XmlElement(name = "Transaction")
private List<Transaction> transaction= null;

public List<Transaction> getTransaction() {
return transaction;
}

public void setTransaction(List<Transaction> transaction) {
this.transaction = transaction;
}


i am getting this as output now



com.progressoft.induction.tp.models.Transaction@0


when i try to do this



transaction.getAmount(); 


then i get null







java xml jaxb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 12:23







Jasbin Karki

















asked Nov 20 '18 at 11:38









Jasbin KarkiJasbin Karki

597




597













  • Try @XmlAttribute instead of @XmlElement

    – Thomas Fritsch
    Nov 20 '18 at 11:45











  • no luck, i am getting null output :(

    – Jasbin Karki
    Nov 20 '18 at 12:20



















  • Try @XmlAttribute instead of @XmlElement

    – Thomas Fritsch
    Nov 20 '18 at 11:45











  • no luck, i am getting null output :(

    – Jasbin Karki
    Nov 20 '18 at 12:20

















Try @XmlAttribute instead of @XmlElement

– Thomas Fritsch
Nov 20 '18 at 11:45





Try @XmlAttribute instead of @XmlElement

– Thomas Fritsch
Nov 20 '18 at 11:45













no luck, i am getting null output :(

– Jasbin Karki
Nov 20 '18 at 12:20





no luck, i am getting null output :(

– Jasbin Karki
Nov 20 '18 at 12:20












1 Answer
1






active

oldest

votes


















0














Code needs minor changes to accommodate list of Transaction against a single Country object.



Class containing List of transactions:



@XmlRootElement(name = "transactions")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transactions
{
@XmlElement(name = "transactions")
private List<Transaction> transactions= null;

public List<Transaction> getTransactions() {
return transactions;
}

public void setTransactions(List<Transaction> transactions) {
this.transactions= transactions;
}
}


Transaction class:



@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transaction
{
private Integer type;
.......


Business logic:



Transactions transactions = (Transactions) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);





share|improve this answer
























  • hi thanks, i am getting com.progressoft.induction.tp.models.Transaction@0 as output now when i try transaction.getAmount() like this i get null output

    – Jasbin Karki
    Nov 20 '18 at 12:18











  • @JasbinKarki need to add BigDecimal Adapter here. Eg. @XmlJavaTypeAdapter(BigDecimalAdaptor.class) private BigDecimal amount;

    – Dark Knight
    Nov 20 '18 at 12:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392206%2funmarshal-an-xml-file-using-jaxb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Code needs minor changes to accommodate list of Transaction against a single Country object.



Class containing List of transactions:



@XmlRootElement(name = "transactions")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transactions
{
@XmlElement(name = "transactions")
private List<Transaction> transactions= null;

public List<Transaction> getTransactions() {
return transactions;
}

public void setTransactions(List<Transaction> transactions) {
this.transactions= transactions;
}
}


Transaction class:



@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transaction
{
private Integer type;
.......


Business logic:



Transactions transactions = (Transactions) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);





share|improve this answer
























  • hi thanks, i am getting com.progressoft.induction.tp.models.Transaction@0 as output now when i try transaction.getAmount() like this i get null output

    – Jasbin Karki
    Nov 20 '18 at 12:18











  • @JasbinKarki need to add BigDecimal Adapter here. Eg. @XmlJavaTypeAdapter(BigDecimalAdaptor.class) private BigDecimal amount;

    – Dark Knight
    Nov 20 '18 at 12:34
















0














Code needs minor changes to accommodate list of Transaction against a single Country object.



Class containing List of transactions:



@XmlRootElement(name = "transactions")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transactions
{
@XmlElement(name = "transactions")
private List<Transaction> transactions= null;

public List<Transaction> getTransactions() {
return transactions;
}

public void setTransactions(List<Transaction> transactions) {
this.transactions= transactions;
}
}


Transaction class:



@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transaction
{
private Integer type;
.......


Business logic:



Transactions transactions = (Transactions) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);





share|improve this answer
























  • hi thanks, i am getting com.progressoft.induction.tp.models.Transaction@0 as output now when i try transaction.getAmount() like this i get null output

    – Jasbin Karki
    Nov 20 '18 at 12:18











  • @JasbinKarki need to add BigDecimal Adapter here. Eg. @XmlJavaTypeAdapter(BigDecimalAdaptor.class) private BigDecimal amount;

    – Dark Knight
    Nov 20 '18 at 12:34














0












0








0







Code needs minor changes to accommodate list of Transaction against a single Country object.



Class containing List of transactions:



@XmlRootElement(name = "transactions")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transactions
{
@XmlElement(name = "transactions")
private List<Transaction> transactions= null;

public List<Transaction> getTransactions() {
return transactions;
}

public void setTransactions(List<Transaction> transactions) {
this.transactions= transactions;
}
}


Transaction class:



@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transaction
{
private Integer type;
.......


Business logic:



Transactions transactions = (Transactions) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);





share|improve this answer













Code needs minor changes to accommodate list of Transaction against a single Country object.



Class containing List of transactions:



@XmlRootElement(name = "transactions")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transactions
{
@XmlElement(name = "transactions")
private List<Transaction> transactions= null;

public List<Transaction> getTransactions() {
return transactions;
}

public void setTransactions(List<Transaction> transactions) {
this.transactions= transactions;
}
}


Transaction class:



@XmlRootElement(name = "employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Transaction
{
private Integer type;
.......


Business logic:



Transactions transactions = (Transactions) jaxbUnmarshaller.unmarshal(file);
System.out.println(transaction);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 11:47









Dark KnightDark Knight

6,33342749




6,33342749













  • hi thanks, i am getting com.progressoft.induction.tp.models.Transaction@0 as output now when i try transaction.getAmount() like this i get null output

    – Jasbin Karki
    Nov 20 '18 at 12:18











  • @JasbinKarki need to add BigDecimal Adapter here. Eg. @XmlJavaTypeAdapter(BigDecimalAdaptor.class) private BigDecimal amount;

    – Dark Knight
    Nov 20 '18 at 12:34



















  • hi thanks, i am getting com.progressoft.induction.tp.models.Transaction@0 as output now when i try transaction.getAmount() like this i get null output

    – Jasbin Karki
    Nov 20 '18 at 12:18











  • @JasbinKarki need to add BigDecimal Adapter here. Eg. @XmlJavaTypeAdapter(BigDecimalAdaptor.class) private BigDecimal amount;

    – Dark Knight
    Nov 20 '18 at 12:34

















hi thanks, i am getting com.progressoft.induction.tp.models.Transaction@0 as output now when i try transaction.getAmount() like this i get null output

– Jasbin Karki
Nov 20 '18 at 12:18





hi thanks, i am getting com.progressoft.induction.tp.models.Transaction@0 as output now when i try transaction.getAmount() like this i get null output

– Jasbin Karki
Nov 20 '18 at 12:18













@JasbinKarki need to add BigDecimal Adapter here. Eg. @XmlJavaTypeAdapter(BigDecimalAdaptor.class) private BigDecimal amount;

– Dark Knight
Nov 20 '18 at 12:34





@JasbinKarki need to add BigDecimal Adapter here. Eg. @XmlJavaTypeAdapter(BigDecimalAdaptor.class) private BigDecimal amount;

– Dark Knight
Nov 20 '18 at 12:34




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392206%2funmarshal-an-xml-file-using-jaxb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word