How to set XML Attribute inside XML Element in Java Pojo Class - JAVX

0 votes

How can I get this Output Response?

<Response>
  <Play loop=10>
    https:aws-mp3-file-locaation.com
  </Play>
</Response>

What I implemented:

1. First Try, Created Simple CustomResponse class and set element and attribute inside that class.

@XmlRootElement(name = "Response")
public CustomResponse{
  private String play;
  private int loop;
  
  
  @XmlElement(name = "Play")
  public String getPlay() {
    return play;
  }

  public void setPlay(String play) {
    this.play = play;
  }

  
  @XmlAttribute(name = "loop")
  public String getLoop() {
    return loop;
  }

  public void setLoop(String loop) {
    this.loop = loop;
  }
}

This is returning the loop value as a response attribute.

<Response loop=10>
  <Play>
    https:aws-mp3-file-locaation.com
  </Play>
</Response>

2. On the second Try, Created a Simple CustomResponse class and Play class. Here I call play inside CustomResponse and In Play.java I already define the element and attribute inside.

@XmlRootElement(name = "Response")
public CustomResponse{
  private Play play;
  
  
  @XmlElement(name = "Play")
  public Play getPlay() {
    return play;
  }

  public void setPlay(Play play) {
    this.play = play;
  }
}
@XmlRootElement(name = "Play")
public PlayResponse{
  private String play;
  private int loop;
  
  
  @XmlElement(name = "play")
  public String getPlay() {
    return play;
  }

  public void setPlay(String play) {
    this.play = play;
  }

  
  @XmlAttribute(name = "loop")
  public String getLoop() {
    return loop;
  }

  public void setLoop(String loop) {
    this.loop = loop;
  }
}

This is returning

<Response>
  <Play>
    <play loop=10>
      https:aws-mp3-file-locaation.com
    </play>
  </Play>
</Response>
Apr 19, 2022 in Java by Rahul
• 3,380 points
2,694 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,120 points
996 views
0 votes
1 answer

How to set Encoding in Java ?

Unfortunately, the file.encoding property has to be specified as ...READ MORE

answered May 29, 2018 in Java by Rishabh
• 3,620 points
15,919 views
0 votes
1 answer

How to read an XML file using XPath in Java?

Import the packages required to work with ...READ MORE

answered Jun 14, 2018 in Java by Akrati
• 3,190 points
3,970 views
0 votes
1 answer
0 votes
1 answer

How to use nested class in Java?

This program will help you understand the ...READ MORE

answered Mar 3, 2019 in Java by Priyaj
• 58,090 points
356 views
0 votes
1 answer

How to parse invalid XML in Java ?

That "XML" is worse than invalid – it's not well-formed. An ...READ MORE

answered Mar 7, 2019 in Java by developer_1
• 3,320 points
5,001 views
0 votes
1 answer

How to set AUTO-SCROLLING of JTextArea in Java GUI?

Hii kartik, When you click anywhere over JTextArea, ...READ MORE

answered Apr 21, 2020 in Java by Niroj
• 82,880 points
1,997 views
0 votes
1 answer

How to set path if java is installed in E drive or D drive rather than c

Hello, @Amazing, It doesn't really matter where you ...READ MORE

answered Sep 1, 2020 in Java by Roshni
• 10,520 points

edited Jul 4, 2023 by Khan Sarfaraz 3,684 views
0 votes
1 answer

How to convert a Set to List in Java?

Hi@MD, The most straightforward way to convert a ...READ MORE

answered Dec 30, 2020 in Java by akhtar
• 38,230 points

edited Jun 22, 2023 by Khan Sarfaraz 407 views
0 votes
1 answer

How to create immutable class in Java?

To create an immutable class in Java, ...READ MORE

answered Oct 16, 2023 in Java by anonymous
• 3,320 points

edited Oct 19, 2023 by anonymous 193 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP