

{"id":92,"date":"2021-01-07T17:14:07","date_gmt":"2021-01-07T09:14:07","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/java\/?p=92"},"modified":"2021-01-07T23:25:43","modified_gmt":"2021-01-07T15:25:43","slug":"jsoup-java-%e8%a7%a3%e6%9e%90-xml","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/java\/post-92.html","title":{"rendered":"jsoup &#8212; java \u89e3\u6790 xml( html )"},"content":{"rendered":"\n<h5>1.\u4e0b\u8f7d Jar \u5305\u5e76\u5bfc\u5165\u5de5\u7a0b\uff0c\u70b9\u51fb<a href=\"https:\/\/pan.5252.online\/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7\/JavaJar\/xml\/jsoup\/\" data-type=\"URL\" data-id=\"https:\/\/pan.5252.online\/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7\/JavaJar\/xml\/jsoup\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u8fd9\u91cc<\/a>\uff08 jsoup-1.11.2.jar\uff09<\/h5>\n\n\n\n<h5>2.\u83b7\u53d6 docment \u5bf9\u8c61<\/h5>\n\n\n\n<p>1.\u901a\u8fc7\u7c7b\u52a0\u8f7d\u5668\uff0c\u83b7\u53d6xml\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String path = JsoupDemo1.class.getClassLoader().getResource(\"student.xml\").getPath();<\/code><\/pre>\n\n\n\n<p>2.\u52a0\u8f7d\u6587\u6863\uff08\u9700\u6307\u5b9a\u7f16\u7801\uff09\u8fdb\u5185\u5b58\uff0c\u83b7\u53d6 document \u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Document document = Jsoup.parse(new File(path), \"utf-8\");<\/code><\/pre>\n\n\n\n<p>3.\u83b7\u53d6\u5143\u7d20\u7684\u503c\uff0c\u5e76\u8fdb\u884c\u64cd\u4f5c<\/p>\n\n\n\n<p>\uff08\u83b7\u53d6 name \u5c5e\u6027\u7684 elements \u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u53d6\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u7684\u503c)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements elements = document.getElementsByTag(\"name\");\nElement element = elements.get(0);\nString name = element.text();\nSystem.out.println(name);<\/code><\/pre>\n\n\n\n<h4 class=\"has-text-align-center\">\u5e38\u7528 5 \u4e2a\u5bf9\u8c61\u7684\u4ecb\u7ecd<\/h4>\n\n\n\n<h5>1.Jsoup \u5bf9\u8c61\uff1a\u5de5\u5177\u7c7b<\/h5>\n\n\n\n<p>\u5177\u4f53\u65b9\u6cd5\uff1aparse\uff1a\u89e3\u6790html\u6216xml\u6587\u6863\uff0c\u8fd4\u56deDocument<\/p>\n\n\n\n<ul><li>parse\u200b(File in, String charsetName)\uff1a\u89e3\u6790xml\u6216html\u6587\u4ef6\u7684\u3002<\/li><li>parse\u200b(String html)\uff1a\u89e3\u6790xml\u6216html\u5b57\u7b26\u4e32<\/li><li>parse\u200b(URL url, int timeoutMillis)\uff1a\u901a\u8fc7\u7f51\u7edc\u8def\u5f84\u83b7\u53d6\u6307\u5b9a\u7684html\u6216xml\u7684\u6587\u6863\u5bf9\u8c61<\/li><\/ul>\n\n\n\n<h5>2.Document\uff1a\u6587\u6863\u5bf9\u8c61\u3002\u4ee3\u8868\u5185\u5b58\u4e2d\u7684dom\u6811<\/h5>\n\n\n\n<p>\u5177\u4f53\u65b9\u6cd5\uff1a\u83b7\u53d6Element\u5bf9\u8c61<\/p>\n\n\n\n<ul><li>getElementById\u200b(String id)\uff1a\u6839\u636eid\u5c5e\u6027\u503c\u83b7\u53d6\u552f\u4e00\u7684element\u5bf9\u8c61<\/li><li>getElementsByTag\u200b(String tagName)\uff1a\u6839\u636e\u6807\u7b7e\u540d\u79f0\u83b7\u53d6\u5143\u7d20\u5bf9\u8c61\u96c6\u5408<\/li><li>getElementsByAttribute\u200b(String key)\uff1a\u6839\u636e\u5c5e\u6027\u540d\u79f0\u83b7\u53d6\u5143\u7d20\u5bf9\u8c61\u96c6\u5408<\/li><li>getElementsByAttributeValue\u200b(String key, String value)\uff1a\u6839\u636e\u5bf9\u5e94\u7684\u5c5e\u6027\u540d\u548c\u5c5e\u6027\u503c\u83b7\u53d6\u5143\u7d20\u5bf9\u8c61\u96c6\u5408<\/li><\/ul>\n\n\n\n<h5>3.Elements\uff1a\u5143\u7d20Element\u5bf9\u8c61\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u5f53\u505a ArrayList\u6765\u4f7f\u7528<\/h5>\n\n\n\n<h5>4.Element\uff1a\u5143\u7d20\u5bf9\u8c61<\/h5>\n\n\n\n<p>\u65b9\u6cd51\uff1a\u83b7\u53d6\u5b50\u5143\u7d20\u5bf9\u8c61<\/p>\n\n\n\n<ul><li>getElementById\u200b(String id)\uff1a\u6839\u636eid\u5c5e\u6027\u503c\u83b7\u53d6\u552f\u4e00\u7684element\u5bf9\u8c61<\/li><li>getElementsByTag\u200b(String tagName)\uff1a\u6839\u636e\u6807\u7b7e\u540d\u79f0\u83b7\u53d6\u5143\u7d20\u5bf9\u8c61\u96c6\u5408<\/li><li>getElementsByAttribute\u200b(String key)\uff1a\u6839\u636e\u5c5e\u6027\u540d\u79f0\u83b7\u53d6\u5143\u7d20\u5bf9\u8c61\u96c6\u5408<\/li><li>getElementsByAttributeValue\u200b(String key, String value)\uff1a\u6839\u636e\u5bf9\u5e94\u7684\u5c5e\u6027\u540d\u548c\u5c5e\u6027\u503c\u83b7\u53d6\u5143\u7d20\u5bf9\u8c61\u96c6\u5408<\/li><\/ul>\n\n\n\n<p>\u65b9\u6cd52\uff1a\u83b7\u53d6\u5c5e\u6027\u503c\uff0cString attr(String key)\uff1a\u6839\u636e\u5c5e\u6027\u540d\u79f0\u83b7\u53d6\u5c5e\u6027\u503c<\/p>\n\n\n\n<p>\u65b9\u6cd53\uff1a\u83b7\u53d6\u6587\u672c\u5185\u5bb9<\/p>\n\n\n\n<ul><li>String text():\u83b7\u53d6\u6587\u672c\u5185\u5bb9<\/li><li>String html():\u83b7\u53d6\u6807\u7b7e\u4f53\u7684\u6240\u6709\u5185\u5bb9(\u5305\u62ec\u5b57\u6807\u7b7e\u7684\u5b57\u7b26\u4e32\u5185\u5bb9)<\/li><\/ul>\n\n\n\n<h5>5.Node\uff1a\u8282\u70b9\u5bf9\u8c61\uff0c\u662fDocument\u548cElement\u7684\u7236\u7c7b<\/h5>\n\n\n\n<h4 class=\"has-text-align-center\">\u5feb\u6377\u67e5\u8be2\u65b9\u5f0f<\/h4>\n\n\n\n<h5>1.selector\u9009\u62e9\u5668<\/h5>\n\n\n\n<p>\u4f7f\u7528\u8bed\u6cd5\uff1a\u53c2\u8003Selector\u7c7b\u4e2d\u5b9a\u4e49\u7684\u8bed\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements element = document.select\u200b(String cssQuery)<\/code><\/pre>\n\n\n\n<h5>2.XPath\uff1aw3c\u63d0\u4f9b\u7684\u5feb\u901f\u67e5\u8be2\u65b9\u6cd5<\/h5>\n\n\n\n<p>1.\u4e0b\u8f7d jar \u5305\u70b9\u51fb<a rel=\"noreferrer noopener\" href=\"https:\/\/pan.5252.online\/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7\/JavaJar\/xml\/jsoup\/\" data-type=\"URL\" data-id=\"https:\/\/pan.5252.online\/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7\/JavaJar\/xml\/jsoup\/\" target=\"_blank\">\u8fd9\u91cc<\/a>\uff08JsoupXpath-0.3.2.jar)\uff0c\u5e76\u5bfc\u5165\u9879\u76ee<\/p>\n\n\n\n<p>2.\u83b7\u53d6 xml \u7684\u8def\u5f84<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String path = JsoupDemo6.class.getClassLoader().getResource(\"student.xml\").getPath();<\/code><\/pre>\n\n\n\n<p>3.\u83b7\u53d6 document \u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Document document = Jsoup.parse(new File(path), \"utf-8\");<\/code><\/pre>\n\n\n\n<p>4.\u6839\u636e document  \u5bf9\u8c61\u83b7\u53d6 JXDocument \u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JXDocument jxDocument = new JXDocument(document);<\/code><\/pre>\n\n\n\n<p>5.\u7ed3\u5408 Xpath \u8bed\u6cd5\u8fdb\u884c\u67e5\u8be2<\/p>\n\n\n\n<p>\uff08\u8bed\u6cd5\u5f88\u591a\uff0c\u641c\u7d22&#8221;XPath\u8bed\u6cd5&#8221;\uff09\uff0c\u8fd9\u91cc\u6f14\u793a\u51e0\u79cd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/1.\u67e5\u8be2\u6240\u6709student\u6807\u7b7e\nList&lt;JXNode&gt; jxNodes = jxDocument.selN(\"\/\/student\");\n\/\/2\u67e5\u8be2\u6240\u6709student\u6807\u7b7e\u4e0b\u7684name\u6807\u7b7e\nList&lt;JXNode&gt; jxNodes2 = jxDocument.selN(\"\/\/student\/name\");\n\/\/3\u67e5\u8be2student\u6807\u7b7e\u4e0b\u5e26\u6709id\u5c5e\u6027\u7684name\u6807\u7b7e\nList&lt;JXNode&gt; jxNodes3 = jxDocument.selN(\"\/\/student\/name&#91;@id]\");\n\/\/4\u67e5\u8be2student\u6807\u7b7e\u4e0b\u5e26\u6709id\u5c5e\u6027\u7684name\u6807\u7b7e \u5e76\u4e14id\u5c5e\u6027\u503c\u4e3a52dixiaowo\nList&lt;JXNode&gt; jxNodes4 = jxDocument.selN(\"\/\/student\/name&#91;@id='52dixiaowo']\");<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u4e0b\u8f7d Jar \u5305\u5e76\u5bfc\u5165\u5de5\u7a0b\uff0c\u70b9\u51fb\u8fd9\u91cc\uff08 jsoup-1.11.2.jar\uff09 2.\u83b7\u53d6 docment \u5bf9\u8c61&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/java\/post-92.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">jsoup &#8212; java \u89e3\u6790 xml( html )<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts\/92"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/java\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}