简体   繁体   中英

Convert xml string to java object : getting `details` as null?

I have been trying to convert xml string to Java object.

There is a root tag of TV . There are multiple fields inside this but I want the whole content as one String.

@XmlRootElement(name = "TV")
@XmlAccessorType(XmlAccessType.FIELD)
public class TV {
    @XmlElement
    public String details;
    public String getDetails() {
        return details;
    }
    public void setDetails(String details) {
        this.details = details;
    }
    @Override
    public String toString() {
        return "TV [details=" + details + "]";
    }   
}


public class XMLtoJavaObject {

    public static void main(String[] args) {
        String xmlString = "<TV version=\"0.91fn\"><channel><title>Friends</title><link>https://www.imdb.com/title/tt0108778/</link><season>2</season></channel></TV>";
        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(TV.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            TV tv = (TV) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));
            System.out.println("TV" + tv);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Output:

TV [details=null]

I am not getting whole nested xml as String.

Can somebody help me what I am missing?

Thanks in advance

Another answser:

TV class:

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

    @XmlAnyElement
    private org.w3c.dom.Element channel;

    @Override
    public String toString() {
      return "TV [channel=" + channel + "]";
    }

    public org.w3c.dom.Element getChannel() {
      return channel;
    }

    public void setChannel(org.w3c.dom.Element channel) {
      this.channel = channel;
    }

}

Then You get text content like this:

System.out.println("text: " +  tv.getChannel().getTextContent());

Output:

text: Friendshttps://www.imdb.com/title/tt0108778/2

Note that it is all texts just concatenated. But having an Element You can iterate recursively through all children and print them together with tag names and text values.

Here is a sample recursive method: channel field name needs to be a Node type:

....
private org.w3c.dom.Node channel;
....

private static String build(Node element) {
  final String result;
  if (element instanceof Text) {
    result = element.getNodeValue();
  } else {
    final StringBuilder sb = new StringBuilder();
    sb.append("<").append(element.getNodeName()).append(">");
    for (int i = 0; i < element.getChildNodes().getLength(); i++) {
      final Node child = element.getChildNodes().item(i);
      sb.append(build(child));
    }
    sb.append("</").append(element.getNodeName()).append(">");
    result = sb.toString();
  }
  return result;
}

Called like this:

  System.out.println(build(tv.getChannel()));

Output:

<channel><title>Friends</title><link>https://www.imdb.com/title/tt0108778/</link><season>2</season></channel>

Try this:

TV class:

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

    @XmlElement
    private Channel channel;

    public Channel getChannel() {
      return channel;
    }

    public void setChannel(Channel channel) {
      this.channel = channel;
    }

    @Override
    public String toString() {
      return "TV [channel=" + channel + "]";
    }
}

Channel class:

@XmlAccessorType(XmlAccessType.FIELD)
public class Channel {
  @XmlElement
  private String title;
  private String link;
  private String season;

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getLink() {
    return link;
  }

  public void setLink(String link) {
    this.link = link;
  }

  public String getSeason() {
    return season;
  }

  public void setSeason(String season) {
    this.season = season;
  }

  @Override
  public String toString() {
    return "Channel [title=" + title + ", link=" + link + ", season=" + season + "]";
  }

}

Output:

TVTV [channel=Channel [title=Friends, link=https://www.imdb.com/title/tt0108778/, season=2]]

And now You can get Your details using Java as You want eg:

tv.getChannel().getTitle();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM