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 in Java by Rahul
• 3,380 points
594 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
667 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
14,832 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,123 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,100 points
205 views
0 votes
1 answer

How to parse invalid XML in Java ?

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

answered Mar 7, 2019 in Java by developer_1
• 3,310 points
3,812 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,800 points
1,015 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
1,569 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,240 points
198 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 67,210 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