unmarshal an xml file using JAXB
[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
add a comment |
[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
Try@XmlAttributeinstead 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
add a comment |
[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
[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
java xml jaxb
edited Nov 20 '18 at 12:23
Jasbin Karki
asked Nov 20 '18 at 11:38
Jasbin KarkiJasbin Karki
597
597
Try@XmlAttributeinstead 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
add a comment |
Try@XmlAttributeinstead 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
add a comment |
1 Answer
1
active
oldest
votes
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);
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Try
@XmlAttributeinstead 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