我的 Web 服务正在运行,我可以使用下面的代码打印出该服务检索到的 XML:
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);
但是,我现在尝试使用此 XML 作为 JTable 的数据提供程序,但我不知道如何解析它。我的代码如下,但无法工作,因为“实体”是一个字符串。
JAXBContext context = JAXBContext.newInstance(Item.class);
Unmarshaller um = context.createUnmarshaller();
Item item = (Item) um.unmarshal(entity);
所以我的问题是
我在这里做错了什么?
我不应该使用 .getEntity(String.Class) 来执行此操作吗?
是否有更简单的方法将此 XML 响应放入 JTable 中?
谢谢
最佳答案
您可以创建 Document使用这样的方法从您的字符串中:
public static Document loadXML(String xmlAsString) {
DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlAsString));
return b.parse(is);
}
然后您可以使用该文档并获取填充 JTable 所需的数据。
关于java - 解析 RESTful XML 响应并在 JTable 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10127677/