Unmrashal nested element with JAXB Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Java inner class and static nested classHow do I break out of nested loops in Java?Use JAXB to create Object from XML StringMOXy @XmlPath ignoredWhy doesnt JAXB send unmarshal events for instances of class created by using mapSimpleTypeDef?JAXB element that is both optional and nillableConvert JAXB class (not its object) to xml templateJava unmarshal string xml to generic type classXML elements send to Spring Boot REST API is not mapped to POJO if xml element naming convention is different to POJO attribute naming conventionJava JAXB exporting hashmaps to xml
Is multiple magic items in one inherently imbalanced?
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
Is there public access to the Meteor Crater in Arizona?
Crossing US/Canada Border for less than 24 hours
How many time has Arya actually used Needle?
Co-worker has annoying ringtone
Is there hard evidence that the grant peer review system performs significantly better than random?
What's the difference between the capability remove_users and delete_users?
Deconstruction is ambiguous
Misunderstanding of Sylow theory
What does Turing mean by this statement?
How does light 'choose' between wave and particle behaviour?
What makes a man succeed?
Getting prompted for verification code but where do I put it in?
Karn the great creator - 'card from outside the game' in sealed
Why are my pictures showing a dark band on one edge?
How would a mousetrap for use in space work?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
Tannaka duality for semisimple groups
Is CEO the "profession" with the most psychopaths?
What initially awakened the Balrog?
Conditions when a permutation matrix is symmetric
Amount of permutations on an NxNxN Rubik's Cube
How to save space when writing equations with cases?
Unmrashal nested element with JAXB
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Java inner class and static nested classHow do I break out of nested loops in Java?Use JAXB to create Object from XML StringMOXy @XmlPath ignoredWhy doesnt JAXB send unmarshal events for instances of class created by using mapSimpleTypeDef?JAXB element that is both optional and nillableConvert JAXB class (not its object) to xml templateJava unmarshal string xml to generic type classXML elements send to Spring Boot REST API is not mapped to POJO if xml element naming convention is different to POJO attribute naming conventionJava JAXB exporting hashmaps to xml
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How to extract data in the nested elements of mentalHealthDays
element ?
Here is my XML:
Here is my code:
@SpringBootApplication
public class DemoApplication
public static void main(String[] args)
try
File file = new File("C:\Users\JFarmer\Projects\demo\xml_wife.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Response customer = (Response) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
catch (JAXBException e)
e.printStackTrace();
@XmlRootElement(name="response")
public class Response
String code;
int fiscalYear;
MentalHealthDays type;
@Override
public String toString()
return "Code=" + code + " , Fiscal Year=" + fiscalYear + " ,test=" + type + "]";
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public MentalHealthDays getType()
return type;
public void setType(MentalHealthDays type)
this.type = type;
Also if you can provide any resources for better understanding, it'd be much appreciated.
I can turn the first couple of elements into variables in my Class, however, I can't figure out how to extract the data in the nested elements into variables. I also have multiple nested elements with the same name.
java xml rest spring-boot jaxb
add a comment |
How to extract data in the nested elements of mentalHealthDays
element ?
Here is my XML:
Here is my code:
@SpringBootApplication
public class DemoApplication
public static void main(String[] args)
try
File file = new File("C:\Users\JFarmer\Projects\demo\xml_wife.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Response customer = (Response) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
catch (JAXBException e)
e.printStackTrace();
@XmlRootElement(name="response")
public class Response
String code;
int fiscalYear;
MentalHealthDays type;
@Override
public String toString()
return "Code=" + code + " , Fiscal Year=" + fiscalYear + " ,test=" + type + "]";
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public MentalHealthDays getType()
return type;
public void setType(MentalHealthDays type)
this.type = type;
Also if you can provide any resources for better understanding, it'd be much appreciated.
I can turn the first couple of elements into variables in my Class, however, I can't figure out how to extract the data in the nested elements into variables. I also have multiple nested elements with the same name.
java xml rest spring-boot jaxb
add a comment |
How to extract data in the nested elements of mentalHealthDays
element ?
Here is my XML:
Here is my code:
@SpringBootApplication
public class DemoApplication
public static void main(String[] args)
try
File file = new File("C:\Users\JFarmer\Projects\demo\xml_wife.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Response customer = (Response) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
catch (JAXBException e)
e.printStackTrace();
@XmlRootElement(name="response")
public class Response
String code;
int fiscalYear;
MentalHealthDays type;
@Override
public String toString()
return "Code=" + code + " , Fiscal Year=" + fiscalYear + " ,test=" + type + "]";
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public MentalHealthDays getType()
return type;
public void setType(MentalHealthDays type)
this.type = type;
Also if you can provide any resources for better understanding, it'd be much appreciated.
I can turn the first couple of elements into variables in my Class, however, I can't figure out how to extract the data in the nested elements into variables. I also have multiple nested elements with the same name.
java xml rest spring-boot jaxb
How to extract data in the nested elements of mentalHealthDays
element ?
Here is my XML:
Here is my code:
@SpringBootApplication
public class DemoApplication
public static void main(String[] args)
try
File file = new File("C:\Users\JFarmer\Projects\demo\xml_wife.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Response customer = (Response) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
catch (JAXBException e)
e.printStackTrace();
@XmlRootElement(name="response")
public class Response
String code;
int fiscalYear;
MentalHealthDays type;
@Override
public String toString()
return "Code=" + code + " , Fiscal Year=" + fiscalYear + " ,test=" + type + "]";
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public MentalHealthDays getType()
return type;
public void setType(MentalHealthDays type)
this.type = type;
Also if you can provide any resources for better understanding, it'd be much appreciated.
I can turn the first couple of elements into variables in my Class, however, I can't figure out how to extract the data in the nested elements into variables. I also have multiple nested elements with the same name.
java xml rest spring-boot jaxb
java xml rest spring-boot jaxb
edited Mar 9 at 10:54
Bharat Chowdary
1258
1258
asked Mar 8 at 22:12
Jess FarmerJess Farmer
6
6
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can create a List
variable holding MentalHealthDays
object, just make sure you name the attribute with the same name as the XMLElements
you want to unsmarshall:
Response element
@XmlRootElement(name="response")
public class Response
private String code;
private int fiscalYear;
private List<MentalHealthDays> mentalHealthDays;
@Override
public String toString()
return "Response" +
"code='" + code + ''' +
", fiscalYear=" + fiscalYear +
", mentalHealthDays=" + mentalHealthDays +
'';
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public List<MentalHealthDays> getMentalHealthDays()
return mentalHealthDays;
public void setMentalHealthDays(List<MentalHealthDays> mentalHealthDays)
this.mentalHealthDays = mentalHealthDays;
Inner class
public class MentalHealthDays
String type;
int visitsAllowed;
int visitsUser;
public String getType()
return type;
public void setType(String type)
this.type = type;
public int getVisitsAllowed()
return visitsAllowed;
public void setVisitsAllowed(int visitsAllowed)
this.visitsAllowed = visitsAllowed;
public int getVisitsUser()
return visitsUser;
public void setVisitsUser(int visitsUser)
this.visitsUser = visitsUser;
@Override
public String toString()
return "MentalHealthDays" +
"type='" + type + ''' +
", visitsAllowed=" + visitsAllowed +
", visitsUser=" + visitsUser +
'';
In classMentalHealthDays
it should bevisitsUsed
instead ofvisitsUser
. AndvisitsLeft
should be added.
– Thomas Fritsch
Mar 11 at 9:53
This worked for me! Thank you, much appreciated. One thing I did have to change though was I had to useprivate List<MentalHealthDays> mentalHealthDays = new ArrayList<MentalHealthDays>();
otherwise I got a null pointer exception
– Jess Farmer
Mar 11 at 13:25
You are welcome, if you dont mind you can mark aswers as right one
– Cristian Colorado
Mar 11 at 13:57
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%2f55071720%2funmrashal-nested-element-with-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
You can create a List
variable holding MentalHealthDays
object, just make sure you name the attribute with the same name as the XMLElements
you want to unsmarshall:
Response element
@XmlRootElement(name="response")
public class Response
private String code;
private int fiscalYear;
private List<MentalHealthDays> mentalHealthDays;
@Override
public String toString()
return "Response" +
"code='" + code + ''' +
", fiscalYear=" + fiscalYear +
", mentalHealthDays=" + mentalHealthDays +
'';
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public List<MentalHealthDays> getMentalHealthDays()
return mentalHealthDays;
public void setMentalHealthDays(List<MentalHealthDays> mentalHealthDays)
this.mentalHealthDays = mentalHealthDays;
Inner class
public class MentalHealthDays
String type;
int visitsAllowed;
int visitsUser;
public String getType()
return type;
public void setType(String type)
this.type = type;
public int getVisitsAllowed()
return visitsAllowed;
public void setVisitsAllowed(int visitsAllowed)
this.visitsAllowed = visitsAllowed;
public int getVisitsUser()
return visitsUser;
public void setVisitsUser(int visitsUser)
this.visitsUser = visitsUser;
@Override
public String toString()
return "MentalHealthDays" +
"type='" + type + ''' +
", visitsAllowed=" + visitsAllowed +
", visitsUser=" + visitsUser +
'';
In classMentalHealthDays
it should bevisitsUsed
instead ofvisitsUser
. AndvisitsLeft
should be added.
– Thomas Fritsch
Mar 11 at 9:53
This worked for me! Thank you, much appreciated. One thing I did have to change though was I had to useprivate List<MentalHealthDays> mentalHealthDays = new ArrayList<MentalHealthDays>();
otherwise I got a null pointer exception
– Jess Farmer
Mar 11 at 13:25
You are welcome, if you dont mind you can mark aswers as right one
– Cristian Colorado
Mar 11 at 13:57
add a comment |
You can create a List
variable holding MentalHealthDays
object, just make sure you name the attribute with the same name as the XMLElements
you want to unsmarshall:
Response element
@XmlRootElement(name="response")
public class Response
private String code;
private int fiscalYear;
private List<MentalHealthDays> mentalHealthDays;
@Override
public String toString()
return "Response" +
"code='" + code + ''' +
", fiscalYear=" + fiscalYear +
", mentalHealthDays=" + mentalHealthDays +
'';
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public List<MentalHealthDays> getMentalHealthDays()
return mentalHealthDays;
public void setMentalHealthDays(List<MentalHealthDays> mentalHealthDays)
this.mentalHealthDays = mentalHealthDays;
Inner class
public class MentalHealthDays
String type;
int visitsAllowed;
int visitsUser;
public String getType()
return type;
public void setType(String type)
this.type = type;
public int getVisitsAllowed()
return visitsAllowed;
public void setVisitsAllowed(int visitsAllowed)
this.visitsAllowed = visitsAllowed;
public int getVisitsUser()
return visitsUser;
public void setVisitsUser(int visitsUser)
this.visitsUser = visitsUser;
@Override
public String toString()
return "MentalHealthDays" +
"type='" + type + ''' +
", visitsAllowed=" + visitsAllowed +
", visitsUser=" + visitsUser +
'';
In classMentalHealthDays
it should bevisitsUsed
instead ofvisitsUser
. AndvisitsLeft
should be added.
– Thomas Fritsch
Mar 11 at 9:53
This worked for me! Thank you, much appreciated. One thing I did have to change though was I had to useprivate List<MentalHealthDays> mentalHealthDays = new ArrayList<MentalHealthDays>();
otherwise I got a null pointer exception
– Jess Farmer
Mar 11 at 13:25
You are welcome, if you dont mind you can mark aswers as right one
– Cristian Colorado
Mar 11 at 13:57
add a comment |
You can create a List
variable holding MentalHealthDays
object, just make sure you name the attribute with the same name as the XMLElements
you want to unsmarshall:
Response element
@XmlRootElement(name="response")
public class Response
private String code;
private int fiscalYear;
private List<MentalHealthDays> mentalHealthDays;
@Override
public String toString()
return "Response" +
"code='" + code + ''' +
", fiscalYear=" + fiscalYear +
", mentalHealthDays=" + mentalHealthDays +
'';
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public List<MentalHealthDays> getMentalHealthDays()
return mentalHealthDays;
public void setMentalHealthDays(List<MentalHealthDays> mentalHealthDays)
this.mentalHealthDays = mentalHealthDays;
Inner class
public class MentalHealthDays
String type;
int visitsAllowed;
int visitsUser;
public String getType()
return type;
public void setType(String type)
this.type = type;
public int getVisitsAllowed()
return visitsAllowed;
public void setVisitsAllowed(int visitsAllowed)
this.visitsAllowed = visitsAllowed;
public int getVisitsUser()
return visitsUser;
public void setVisitsUser(int visitsUser)
this.visitsUser = visitsUser;
@Override
public String toString()
return "MentalHealthDays" +
"type='" + type + ''' +
", visitsAllowed=" + visitsAllowed +
", visitsUser=" + visitsUser +
'';
You can create a List
variable holding MentalHealthDays
object, just make sure you name the attribute with the same name as the XMLElements
you want to unsmarshall:
Response element
@XmlRootElement(name="response")
public class Response
private String code;
private int fiscalYear;
private List<MentalHealthDays> mentalHealthDays;
@Override
public String toString()
return "Response" +
"code='" + code + ''' +
", fiscalYear=" + fiscalYear +
", mentalHealthDays=" + mentalHealthDays +
'';
@XmlElement
public String getCode()
return code;
public void setCode(String code)
this.code = code;
@XmlElement
public int getFiscalYear()
return fiscalYear;
public void setFiscalYear(int fiscalYear)
this.fiscalYear = fiscalYear;
@XmlElement
public List<MentalHealthDays> getMentalHealthDays()
return mentalHealthDays;
public void setMentalHealthDays(List<MentalHealthDays> mentalHealthDays)
this.mentalHealthDays = mentalHealthDays;
Inner class
public class MentalHealthDays
String type;
int visitsAllowed;
int visitsUser;
public String getType()
return type;
public void setType(String type)
this.type = type;
public int getVisitsAllowed()
return visitsAllowed;
public void setVisitsAllowed(int visitsAllowed)
this.visitsAllowed = visitsAllowed;
public int getVisitsUser()
return visitsUser;
public void setVisitsUser(int visitsUser)
this.visitsUser = visitsUser;
@Override
public String toString()
return "MentalHealthDays" +
"type='" + type + ''' +
", visitsAllowed=" + visitsAllowed +
", visitsUser=" + visitsUser +
'';
answered Mar 8 at 23:21
Cristian ColoradoCristian Colorado
1,57821322
1,57821322
In classMentalHealthDays
it should bevisitsUsed
instead ofvisitsUser
. AndvisitsLeft
should be added.
– Thomas Fritsch
Mar 11 at 9:53
This worked for me! Thank you, much appreciated. One thing I did have to change though was I had to useprivate List<MentalHealthDays> mentalHealthDays = new ArrayList<MentalHealthDays>();
otherwise I got a null pointer exception
– Jess Farmer
Mar 11 at 13:25
You are welcome, if you dont mind you can mark aswers as right one
– Cristian Colorado
Mar 11 at 13:57
add a comment |
In classMentalHealthDays
it should bevisitsUsed
instead ofvisitsUser
. AndvisitsLeft
should be added.
– Thomas Fritsch
Mar 11 at 9:53
This worked for me! Thank you, much appreciated. One thing I did have to change though was I had to useprivate List<MentalHealthDays> mentalHealthDays = new ArrayList<MentalHealthDays>();
otherwise I got a null pointer exception
– Jess Farmer
Mar 11 at 13:25
You are welcome, if you dont mind you can mark aswers as right one
– Cristian Colorado
Mar 11 at 13:57
In class
MentalHealthDays
it should be visitsUsed
instead of visitsUser
. And visitsLeft
should be added.– Thomas Fritsch
Mar 11 at 9:53
In class
MentalHealthDays
it should be visitsUsed
instead of visitsUser
. And visitsLeft
should be added.– Thomas Fritsch
Mar 11 at 9:53
This worked for me! Thank you, much appreciated. One thing I did have to change though was I had to use
private List<MentalHealthDays> mentalHealthDays = new ArrayList<MentalHealthDays>();
otherwise I got a null pointer exception– Jess Farmer
Mar 11 at 13:25
This worked for me! Thank you, much appreciated. One thing I did have to change though was I had to use
private List<MentalHealthDays> mentalHealthDays = new ArrayList<MentalHealthDays>();
otherwise I got a null pointer exception– Jess Farmer
Mar 11 at 13:25
You are welcome, if you dont mind you can mark aswers as right one
– Cristian Colorado
Mar 11 at 13:57
You are welcome, if you dont mind you can mark aswers as right one
– Cristian Colorado
Mar 11 at 13:57
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%2f55071720%2funmrashal-nested-element-with-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