Parse a document from a String(将一个字符串解析成文档对象)

本文介绍了如何使用Java解析HTML字符串,包括解析方法的使用、描述解析过程以及获取数据的方法。
[size=large]Problem[/size]
You have HTML in a Java String(你有一个java字符串格式的html), and you want to parse that HTML to get at its contents(和 你需要解析这个html获得他的内容), or to make sure it's well formed(或者确保他是格式良好的), or to modify it(或者需要修改他). The String may have come from user input(这个字符串可以来自用户输入), a file(一个文件), or from the web(或者一个网站).

[size=large]Solution(解决方案)[/size]
Use the static Jsoup.parse(String html) method(使用静态的Jsoup.parse(String html)), or Jsoup.parse(String html, String baseUri) if the page came from the web(如果字符串来自页面), and you want to get at absolute URLs (和你需要获得里面的绝对路径)(see [working-with-urls]).

String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);

[size=large]Description(描述)[/size]
The parse(String html, String baseUri) method parses the input HTML into a new Document()(这个parse(String html,String baseUti) 方法解析输入的html到一个新的Document()对象中). The base URI argument is used to resolve relative URLs into absolute URLs(这个baseURI参数是来解析相对路径到绝对路径的), and should be set to the URL where the document was fetched from(并且应该设置这份文件是取数据取自那里的url). If that's not applicable(如果这是不适用的), or if you know the HTML has a base element(或者你不知道这个html源是那里), you can use the parse(String html) method(你可以使用parse(String html) 方法).

As long as you pass in a non-null string(只要你传入一个非空字符串), you're guaranteed to have a successful(你得保证有一个成功的), sensible parse(明智的解析), with a Document containing (at least) a head and a body element(文档包(至少)含头head和body元素). (BETA: if you do get an exception raised, or a bad parse-tree, please file a bug.)

Once you have a Document(一旦你有了一个文档), you can get get at the data using the appropriate methods in Document and its supers Element and Node(你可以得到所需的数据使用适当的方法在文档及其管理员元素和节点).
在Android中,将XML格式的数据转换为JSON格式,通常需要借助于第三方库,如Gson、Jackson或Android自带的`org.json`包。以下是一个基本步骤: 1. **添加依赖库**: - 如果使用Gson,可以在build.gradle文件中添加依赖: ```gradle implementation 'com.google.code.gson:gson:2.8.7' ``` - 使用`org.json`的话,则无需外部库,因为它已经内置于Android SDK。 2. **创建JSONObject或JSONArray实例**: ```java // 使用Gson Gson gson = new Gson(); String xmlString = ...; // 从XML源获取内容 Type listType = new TypeToken<List<MyJsonObject>>(){}.getType(); // 假设你的XML结构映射到一个列表 List<MyJsonObject> jsonArray = gson.fromJson(xmlString, listType); // 或者使用org.json JSONObject jsonObject; try { jsonObject = new JSONObject(xmlString); } catch (JSONException e) { e.printStackTrace(); } ``` 3. **XML解析**: 你可以使用`android.util.Xml`类或者第三方库如`javax.xml.parsers`来解析XML字符串。这里假设你的XML布局已经被解析了`Element`对象: ```java DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xmlString)); Document doc = dBuilder.parse(inputSource); Element root = doc.getDocumentElement(); ``` 4. **遍历XML并转换**: 对于每个需要转换的节点,将其数据提取出来,然后放入相应的目标对象(如JSONObject的键值对或JSONArray的元素)中。 5. **构建JSON字符串**: ```java // Gson示例 String jsonString = gson.toJson(jsonArray); // org.json示例 try { jsonString = jsonObject.toString(); } catch (JSONException e) { e.printStackTrace(); } ``` 记得处理可能出现的异常,并且调整代码以适应你的具体XML结构和目标JSON结构。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值