解析JAVA中JSON文件中包含的XML

时间:2015-04-26 14:10:17

标签: java xml json

我请求一个提供类似JSON文件的API:

{
   "topics": [],
   "dictionaryCode": "english-french",
   "entryLabel": "cat",
   "entryContent": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<entry id=\"cat_1\" idm_id=\"000009987\" lang=\"en-gb\"> <form> <orth>cat<\/orth> <span> [<\/span> <pron type=\"\">ˈkæt<audio type=\"pronunciation\" title=\"cat\"> <source type=\"audio/mpeg\" src=\"https://api.collinsdictionary.com/media/sounds/sounds/0/081/08189/08189.mp3\"/>Your browser does not support HTML5 audio.<\/audio> <\/pron> <span>]<\/span> <\/form> <hom id=\"cat_1.1\"> <span>   <\/span> <gramGrp> <pos>noun<\/pos> <\/gramGrp> <sense n=\"1\"> <span>   <\/span> <span class=\"bold\">1 <\/span> <lbl type=\"misc\"> <span>(<\/span>domestic<span>)<\/span> <\/lbl> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>chat <hi rend=\"i\">m<\/hi> <\/quote> <\/cit> <cit id=\"cat_1.2\" type=\"example\"> <span>;   <\/span> <quote>Have you got a cat?<\/quote> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>Est-ce que tu as un chat?<\/quote> <\/cit> <\/cit> <re id=\"cat_1.3\" type=\"phr\"> <span>;   <\/span> <form type=\"phr\"> <orth>to let the cat out of the bag<\/orth> <\/form> <sense> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>vendre la mèche<\/quote> <\/cit> <\/sense> <\/re> <re id=\"cat_1.4\" type=\"phr\"> <span>;   <\/span> <form type=\"phr\"> <orth>curiosity killed the cat<\/orth> <\/form> <sense> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>la curiosité est toujours punie<\/quote> <\/cit> <\/sense> <\/re> <re id=\"cat_1.5\" type=\"phr\"> <span>;   <\/span> <form type=\"phr\"> <orth>to look like sth the cat dragged in<\/orth> <\/form> <form type=\"phr\"> <span>, <\/span> <orth>to look like sth the cat brought in<\/orth> <\/form> <sense> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>être dans un état lamentable<\/quote> <\/cit> <\/sense> <\/re> <re id=\"cat_1.6\" type=\"phr\"> <span>;   <\/span> <form type=\"phr\"> <orth>to play cat and mouse with sb<\/orth> <\/form> <form type=\"phr\"> <span>, <\/span> <orth>to play a game of cat and mouse with sb<\/orth> <\/form> <sense> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>jouer au chat et à la souris avec qn<\/quote> <\/cit> <\/sense> <\/re> <re id=\"cat_1.7\" type=\"phr\"> <span>;   <\/span> <form type=\"phr\"> <orth>to put the cat among the pigeons<\/orth> <\/form> <form type=\"phr\"> <span>, <\/span> <orth>to set the cat among the pigeons<\/orth> <\/form> <lbl type=\"geo\"> <span> (<\/span>British<span>)<\/span> <\/lbl> <sense> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>jeter un pavé dans la mare<\/quote> <\/cit> <\/sense> <\/re> <re id=\"cat_1.8\" type=\"phr\"> <span>;   <\/span> <form type=\"phr\"> <orth>there's no room to swing a cat<\/orth> <\/form> <sense> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>on ne peut pas se tourner<\/quote> <\/cit> <\/sense> <\/re> <\/sense> <sense n=\"2\"> <span> <br/> <\/span> <span class=\"bold\">2 <\/span> <lbl type=\"syn\"> <span>(= <\/span>big cat<span>)<\/span> <\/lbl> <span> <\/span> <cit type=\"translation\" lang=\"fr\"> <quote>félin <hi rend=\"i\">m<\/hi> <\/quote> <\/cit> <cit id=\"cat_1.9\" type=\"example\"> <span>;   <\/span> <\/cit> <\/sense> <\/hom> <form type=\"inflected\"> <orth>cats<\/orth> <\/form> <\/entry>\n",
   "entryUrl": "http://api.collinsdictionary.com/api/v1/dictionaries/english-french/entries/cat_1",
   "format": "xml",
   "entryId": "cat_1"
}

现在我的JSON文件存储在JAVA中:

JSONObject bestMatch = new JSONObject(api.searchFirst("cat"));

但是现在我需要解析这个JSON文件以获取entryContent值,这是一个XML文件以便解析它。

在JAVA中这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:1)

我已经使用了这个基于DOM解析器的JAVA类:

public static Document loadXMLFromString(String xml) throws Exception
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    return builder.parse(is);
}

您还可以看到此question

然后我只需加载我的xml文档:

Document document = loadXMLFromString(xmlContent);