{"id":1459,"date":"2016-11-16T11:56:35","date_gmt":"2016-11-16T02:56:35","guid":{"rendered":"http:\/\/www.digifie.jp\/blog\/?p=1459"},"modified":"2016-11-16T11:59:56","modified_gmt":"2016-11-16T02:59:56","slug":"python-%e3%81%a7%e3%81%ae-xml-%e3%83%91%e3%83%bc%e3%82%b9%e3%82%92%e3%81%99%e3%81%90%e5%bf%98%e3%82%8c%e3%82%8b%e3%81%ae%e3%81%a7%e3%83%a1%e3%83%a2","status":"publish","type":"post","link":"https:\/\/www.digifie.jp\/blog\/archives\/1459","title":{"rendered":"Python \u3067 XML \u3092\u4f7f\u3046\u3068\u304d\u3044\u308d\u3044\u308d\u5fd8\u308c\u3066\u308b\u306e\u3067\u30e1\u30e2"},"content":{"rendered":"<div class='wp_social_bookmarking_light'>        <div class=\"wsbl_facebook\"><a href='http:\/\/www.facebook.com\/share.php?u=https%3A%2F%2Fwww.digifie.jp%2Fblog%2Farchives%2F1459&t=Python%20%E3%81%A7%20XML%20%E3%82%92%E4%BD%BF%E3%81%86%E3%81%A8%E3%81%8D%E3%81%84%E3%82%8D%E3%81%84%E3%82%8D%E5%BF%98%E3%82%8C%E3%81%A6%E3%82%8B%E3%81%AE%E3%81%A7%E3%83%A1%E3%83%A2' title='Facebook \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/www.digifie.jp\/blog\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/facebook.png' alt='Facebook \u306b\u30b7\u30a7\u30a2' title='Facebook \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.digifie.jp\/blog\/archives\/1459\" data-text=\"Python \u3067 XML \u3092\u4f7f\u3046\u3068\u304d\u3044\u308d\u3044\u308d\u5fd8\u308c\u3066\u308b\u306e\u3067\u30e1\u30e2\" data-via=\"LLC DigiFie\" data-lang=\"ja\">Tweet<\/a><\/div><\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n<p>Python \u3067 XML \u30d1\u30fc\u30b9\u3059\u308b\u306b\u306f\u3001Python2.5 \u304b\u3089\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u5165\u3063\u3066\u3044\u308b ElementTree \u3092\u5229\u7528\u3002<\/p>\n<p>\u3000<br \/>\nElementTree\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30dd\u30fc\u30c8<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; from xml.etree import ElementTree\r\n<\/pre>\n<p>\u30b5\u30f3\u30d7\u30ebXML\uff08hage.xml\uff09<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;hage&gt;\r\n\t&lt;level name=&quot;angel&quot; job=&quot;comedian&quot; stat=&quot;now&quot;&gt;6&lt;\/level&gt;\r\n\t&lt;level name=&quot;math&quot; job=&quot;politician&quot; stat=&quot;former&quot;&gt;7&lt;\/level&gt;\r\n\t&lt;level name=&quot;blies&quot; job=&quot;actor&quot; stat=&quot;now&quot;&gt;8&lt;\/level&gt;\r\n\t&lt;level name=&quot;saitama&quot; job=&quot;hero&quot; stat=&quot;now&quot;&gt;10&lt;\/level&gt;\r\n&lt;\/hage&gt;\r\n<\/pre>\n<p>\u3000<\/p>\n<p>XML\u30d5\u30a1\u30a4\u30eb\u8aad\u307f\u8fbc\u307f<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; tree = ElementTree.parse('hage.xml')\r\n&gt;&gt;&gt; tree\r\n&lt;xml.etree.ElementTree.ElementTree object at 0x10c5396d0&gt;\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u30eb\u30fc\u30c8\u30ce\u30fc\u30c9\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; root = tree.getroot()\r\n&gt;&gt;&gt; root\r\n&lt;Element 'hage' at 0x10c55e850&gt;\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u30eb\u30fc\u30c8\u306e\u30bf\u30b0\u540d\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; root.tag\r\n'hage'\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u30eb\u30fc\u30c8\u306e\u5c5e\u6027\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; root.attrib\r\n{} # \u306a\u3057\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u5b50\u8981\u7d20\u306e\u30bf\u30b0\u540d\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; for e in root:\r\n... \te.tag\r\n'level'\r\n'level'\r\n'level'\r\n'level'\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u5b50\u8981\u7d20\u306e\u5c5e\u6027\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; for e in root:\r\n... \te.attrib \r\n{'stat': 'now', 'job': 'comedian', 'name': 'angel'}\r\n{'stat': 'former', 'job': 'politician', 'name': 'math'}\r\n{'stat': 'now', 'job': 'actor', 'name': 'blies'}\r\n{'stat': 'now', 'job': 'hero', 'name': 'saitama'}\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u5b50\u8981\u7d20\u306e\u5c5e\u6027\u540d\u3092\u6307\u5b9a\u3057\u3066\u5024\u3092\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; for e in root:\r\n... \te.get('name')\r\n'angel'\r\n'math'\r\n'blies'\r\n'saitama'\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u5b50\u8981\u7d20\u306e\u5024\u3092\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; for e in root:\r\n... \te.text\r\n'6'\r\n'7'\r\n'8'\r\n'10'\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u3000<br \/>\n\u25a0 findall(\u30bf\u30b0\u540d) \u3092\u4f7f\u7528\u3059\u308b\u5834\u5408<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; root.findall('level')\r\n[&lt;Element 'level' at 0x10c55eed0&gt;, &lt;Element 'level' at 0x10c55ef10&gt;, &lt;Element 'level' at 0x10c55efd0&gt;, &lt;Element 'level' at 0x10c5670d0&gt;]\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u5b50\u8981\u7d20\u306e\u5c5e\u6027\u540d\u3092\u6307\u5b9a\u3057\u3066\u5024\u3092\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfor e in root.findall('level'):  \r\n... \te.get('name')\r\n'angel'\r\n'math'\r\n'blies'\r\n'saitama'\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u5b50\u8981\u7d20\u306e\u5024\u3092\u53d6\u5f97<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfor e in root.findall('level'):  \r\n... \te.text\r\n'6'\r\n'7'\r\n'8'\r\n'10'\r\n<\/pre>\n<p>\u3000<\/p>\n<p>\u8981\u7d20\u306b\u5024\u3092\u66f8\u304d\u8fbc\u3080<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; root.findall('level')[1].text # \u73fe\u5728\u306e\u5024\r\n'7'\r\n&gt;&gt;&gt; root.findall('level')[1].text= '8' # \u66f8\u304d\u63db\u3048\r\n&gt;&gt;&gt; root.findall('level')[1].text \r\n'8'\r\n<\/pre>\n<p>\u3000<\/p>\n<p>XML\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u4fdd\u5b58<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; tree.write('hage1.xml')\r\n<\/pre>\n<p>\u3000<\/p>\n<div class='wp_social_bookmarking_light'>        <div class=\"wsbl_facebook\"><a href='http:\/\/www.facebook.com\/share.php?u=https%3A%2F%2Fwww.digifie.jp%2Fblog%2Farchives%2F1459&t=Python%20%E3%81%A7%20XML%20%E3%82%92%E4%BD%BF%E3%81%86%E3%81%A8%E3%81%8D%E3%81%84%E3%82%8D%E3%81%84%E3%82%8D%E5%BF%98%E3%82%8C%E3%81%A6%E3%82%8B%E3%81%AE%E3%81%A7%E3%83%A1%E3%83%A2' title='Facebook \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/www.digifie.jp\/blog\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/facebook.png' alt='Facebook \u306b\u30b7\u30a7\u30a2' title='Facebook \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.digifie.jp\/blog\/archives\/1459\" data-text=\"Python \u3067 XML \u3092\u4f7f\u3046\u3068\u304d\u3044\u308d\u3044\u308d\u5fd8\u308c\u3066\u308b\u306e\u3067\u30e1\u30e2\" data-via=\"LLC DigiFie\" data-lang=\"ja\">Tweet<\/a><\/div><\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Python \u3067 XML \u30d1\u30fc\u30b9\u3059\u308b\u306b\u306f\u3001Python2.5 \u304b\u3089\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u5165\u3063\u3066\u3044\u308b ElementTree \u3092\u5229\u7528\u3002 \u3000 ElementTree\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30dd\u30fc\u30c8 \u30b5\u30f3\u30d7\u30ebXML\uff08hage.xml\uff09 \u3000 X [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts\/1459"}],"collection":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/comments?post=1459"}],"version-history":[{"count":13,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts\/1459\/revisions"}],"predecessor-version":[{"id":1472,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts\/1459\/revisions\/1472"}],"wp:attachment":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/media?parent=1459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/categories?post=1459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/tags?post=1459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}