1

My web service is working and I can print out the XML retrieved by the Service with my code below:

resource = client.resource("http://localhost:8080/testProject/rest/items");
ClientResponse response= resource.get(ClientResponse.class);
String entity = response.getEntity(String.class);
System.out.println(entity);

However I'm trying to now use this XML to be the data provider for a JTable, and I can't work out how to parse it. My code is below but won't work because "entity" is a string.

    JAXBContext context = JAXBContext.newInstance(Item.class);
    Unmarshaller um = context.createUnmarshaller();
    Item item = (Item) um.unmarshal(entity);

So my question(s) are

What am I doing wrong here?

Should I not be using .getEntity(String.Class) to do this?

Is there an easier way to get this XML response into a JTable?

Thanks

abarisone
  • 3,707
  • 11
  • 35
  • 54
flexer7661
  • 97
  • 2
  • 9

3 Answers3

2

You can create a Document from your String using a method like that:

 public static Document loadXML(String xmlAsString) {
    DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xmlAsString));
    return b.parse(is);
}

Then you can use that Document and get the data you need to populate your JTable.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
0

first, don't convert xml to String data unless absolutely necessary (it's fine for debug output, but not for real work). i haven't used jersey personally, but i'd imagine you can get the xml in a better form than a String (e.g. a DOM Document).

however, if you must get the xml from a string, then you can unmarshal it like so:

um.unmarshal(new StringReader(entity));
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Thanks for your reply, String was the only method I knew. I wonder if anyone has any advice on what to use instead of String? Not really sure on the DOM document as I thought these aren't needed as i'm using JAX-B – flexer7661 Apr 12 '12 at 17:55
  • If you do , then Jersey will do all dirty work for you (provided you have correctly annotated class `Item`). See [documentation](http://jersey.java.net/nonav/documentation/latest/xml.html#d4e820). – dma_k Apr 15 '12 at 20:27
0

Read the response as a Document

Document xml = wsResponse.asXml();
displayXml(xml);

Use below code snippet after that for text conversion I used the ref

void displayXml(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e1) {
        e1.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    System.out.println(output);
}
Community
  • 1
  • 1
Ram
  • 1,154
  • 6
  • 22