gpt4 book ai didi

java - 用JAVA解析JSON文件中包含的XML

转载 作者:行者123 更新时间:2023-12-01 11:36:52 25 4
gpt4 key购买 nike

我请求一个提供 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 中执行此操作的最佳方法是什么?

最佳答案

我使用了这个基于 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);

关于java - 用JAVA解析JSON文件中包含的XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29878682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com