{"id":2249,"date":"2016-05-14T15:52:07","date_gmt":"2016-05-14T07:52:07","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2249"},"modified":"2016-05-14T15:52:07","modified_gmt":"2016-05-14T07:52:07","slug":"%e7%94%a8xmltodict%e8%a7%a3%e6%9e%90xml%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2249.html","title":{"rendered":"\u7528xmltodict\u89e3\u6790XML\u6587\u4ef6"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u6700\u8fd1\u4e00\u6bb5\u65f6\u95f4\u5728\u4f7f\u7528<a href=\"https:\/\/github.com\/robertdavidgraham\/masscan\" target=\"_blank\">Masscan<\/a>\u8fd9\u4e2a\u8d85\u7ea7\u7aef\u53e3\u626b\u63cf\u5668\u505a\u626b\u63cf\uff0c\u626b\u63cf\u5b8c\u4e86\u4e4b\u540e\u5c31\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u90fd\u662f\u7528XML\u6587\u4ef6\u6765\u5b58\u653e\u626b\u63cf\u7ed3\u679c\uff08\u867d\u7136\u8bf4\u6211\u81ea\u5df1\u66f4\u504f\u5411\u4e8e\u7528list\u683c\u5f0f\u5b58\u653e\u7ed3\u679c\uff09\uff0c\u6bd4\u5982\uff1a<a href=\"https:\/\/github.com\/maK-\/scantastic-tool\" target=\"_blank\">scantastic-tool<\/a>\u00a0\u8fd9\u4e2a\u5de5\u5177\u662f\u7528Python\u5199\u7684\uff0c\u8c03\u7528Masscan\/Nmap\u8fdb\u884c\u7aef\u53e3\u626b\u63cf\uff0c\u7136\u540e\u7528<a href=\"https:\/\/github.com\/martinblech\/xmltodict\" target=\"_blank\">xmltodict<\/a>\u8fd9\u4e2a\u6a21\u5757\u89e3\u6790\u4ea7\u751f\u7684XML\u6587\u4ef6\uff0c\u5e76\u5c06\u89e3\u6790\u7ed3\u679c\u5bfc\u5165ElasticSearch\uff0c\u6700\u540e\u7528Kibana\u8fdb\u884c\u5c55\u793a\u3002\u56e0\u4e3a\u611f\u89c9\u89e3\u6790XML\u6587\u4ef6\u7684\u529f\u80fd\u662f\u5f88\u5e38\u7528\u7684\uff0c\u6240\u4ee5\u53bb\u5b66\u4e60\u4e86\u4e00\u4e0bxmltodict\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u611f\u89c9\u633a\u4e0d\u9519\u7684\u3002<\/p>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>#\u5c06XML\u89e3\u6790\u6210OrderedDict<\/p>\n<pre class=\"lang:default decode:true \">import xmltodict, json\n\nmyxml = \"\"\"&lt;mydocument has=\"an attribute\"&gt;\n  &lt;and&gt;\n    &lt;many&gt;elements&lt;\/many&gt;\n    &lt;many&gt;more elements&lt;\/many&gt;\n  &lt;\/and&gt;\n  &lt;plus a=\"complex\"&gt;\n    element as well\n  &lt;\/plus&gt;\n&lt;\/mydocument&gt;\"\"\"\n\nmyxml_dict = xmltodict.parse(myxml)  #\u7528 parse \u8fdb\u884c\u89e3\u6790\nprint json.dumps(myxml_dict)  #\u89e3\u6790\u540e\u7684\u5185\u5bb9\u53ef\u4ee5\u76f4\u63a5\u7528json.dumps\u5904\u7406\nprint myxml_dict['mydocument']['@has']            # == u'an attribute'  \u300c@\u5f00\u5934\u7684\u662f\u6307Element\u7684\u5c5e\u6027\u300d\nprint myxml_dict['mydocument']['and']['many']   # == [u'elements', u'more elements']\nprint myxml_dict['mydocument']['plus']['@a']      # == u'complex'\nprint myxml_dict['mydocument']['plus']['#text']   # == u'element as well'  \u300c#\u5f00\u5934\u7684\u662f\u6307Element\u7684\u503c\u300d<\/pre>\n<p>#\u5c06dict\u8f6c\u6362\u6210XML\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/p>\n<pre class=\"lang:default decode:true \">mydict = {\n    'text': {\n        '@color':'red',\n        '@stroke':'2',\n        '#text':'This is a test'\n    }\n}\n\nmydict_xml = xmltodict.unparse(mydict, pretty=True)  #\u7528 unparse \u53ef\u4ee5\u5c06dict\u8f6c\u6362\u6210XML\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\nprint mydict_xml\n\"\"\"\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;text stroke=\"2\" color=\"red\"&gt;This is a test&lt;\/text&gt;\n\"\"\"\nprint xmltodict.unparse(mydict, pretty=True)<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"https:\/\/github.com\/martinblech\/xmltodict\">https:\/\/github.com\/martinblech\/xmltodict<\/a><\/li>\n<li><a href=\"https:\/\/pypi.python.org\/pypi\/xmltodict\">https:\/\/pypi.python.org\/pypi\/xmltodict<\/a><\/li>\n<li><a href=\"http:\/\/docs.python-guide.org\/en\/latest\/scenarios\/xml\/\">http:\/\/docs.python-guide.org\/en\/latest\/scenarios\/xml\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/17177109\/convert-xml-to-python-dict\">http:\/\/stackoverflow.com\/questions\/17177109\/convert-xml-to-python-dict<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/26926426\/parsing-an-xml-file-with-an-ordered-dictionary\">http:\/\/stackoverflow.com\/questions\/26926426\/parsing-an-xml-file-with-an-ordered-dictionary<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6700\u8fd1\u4e00\u6bb5\u65f6\u95f4\u5728\u4f7f\u7528Masscan\u8fd9\u4e2a\u8d85\u7ea7\u7aef\u53e3\u626b\u63cf\u5668\u505a\u626b\u63cf\uff0c\u626b\u63cf\u5b8c\u4e86\u4e4b\u540e\u5c31\u662f\u6570\u636e\u5904\u7406\u548c\u5206 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[8,599,600],"class_list":["post-2249","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-python","tag-xml","tag-xmltodict"],"views":5307,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2249"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2249\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}