{"id":3988,"date":"2016-05-10T01:33:48","date_gmt":"2016-05-09T16:33:48","guid":{"rendered":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=3988"},"modified":"2016-10-17T08:04:55","modified_gmt":"2016-10-17T08:04:55","slug":"php%e3%81%a7xml%e3%82%92dom%e3%81%a8%e3%81%97%e3%81%a6%e6%89%b1%e3%81%86","status":"publish","type":"post","link":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=3988","title":{"rendered":"PHP\u3067XML\u3092DOM\u3068\u3057\u3066\u6271\u3046"},"content":{"rendered":"<p>\u30bf\u30a4\u30c8\u30eb\u304c\u9069\u5f53\u306a\u306e\u304b\u3069\u3046\u304b\u5206\u304b\u308a\u307e\u305b\u3093\u304c\u3001SimpleXML \u3067\u306f\u6271\u3044\u306b\u304f\u3044\u51e6\u7406\u304c\u3042\u3063\u3066\u3001PHP \u3067 DOM \u3092\u64cd\u4f5c\u3059\u308b\u65b9\u6cd5\u3092\u5b66\u3093\u3067\u307f\u307e\u3057\u305f\u3002\u307e\u305a\u3001\u6b32\u3057\u304b\u3063\u305f\u6a5f\u80fd\u306f\u3001\u30c6\u30ad\u30b9\u30c8\u3067\u7d44\u307f\u4e0a\u3052\u305fXML\u306e\u8981\u7d20\u3092\u3001DOM\u306b\u7d44\u307f\u8fbc\u3080\u3068\u3044\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">$xmltext = &lt;&lt;&lt; end_of_quote\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;root&gt;\r\n&lt;\/root&gt;\r\nend_of_quote;\r\n$xml = new DOMDocument();\r\n\/\/$xml-&gt;formatOutput = true;\r\n$xml-&gt;loadXML($xmltext);\r\n$root = $xml-&gt;getElementsByTagName(&quot;root&quot;)-&gt;item(0);\r\n$f = $xml-&gt;createDocumentFragment();\r\n$f-&gt;appendXML(&quot;&lt;node&gt;\\n&lt;a&gt;hello&lt;\/a&gt;\\n&lt;\/node&gt;&quot;);\r\n$h = $xml-&gt;documentElement-&gt;appendChild($f);\r\n$g = new DOMText(&quot;\\n\\n&quot;);\r\n$root-&gt;appendChild($g);\r\n\r\n\r\necho $xml-&gt;saveXML(); \r\n\r\n<\/code><\/pre>\n<p>\u6539\u884c\u3092\u66f8\u304d\u8fbc\u3093\u3067\u6574\u5f62\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u4ed6\u306b\u65b9\u6cd5\u304c\u306a\u3044\u3082\u306e\u3067\u3057\u3087\u3046\u304b\u3002formatOutput \u304c\u5229\u304d\u307e\u305b\u3093\u3002createDocumentFragment \u304c\u4f55\u306a\u306e\u304b\u304c\u5206\u304b\u308a\u307e\u305b\u3093\u3002\u3053\u306e\u51fa\u529b\u306f\u4e0b\u8a18\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;root&gt;\r\n&lt;node&gt;\r\n&lt;a&gt;hello&lt;\/a&gt;\r\n&lt;\/node&gt;\r\n&lt;\/root&gt; \r\n<\/code><\/pre>\n<p>\u3082\u3046\u3072\u3068\u3064\u5fc5\u8981\u3068\u306a\u3063\u3066\u3044\u308b\u6a5f\u80fd\u306f\u3001XML\u8981\u7d20\u3078\u306e\u30bf\u30b0\u306e\u8ffd\u52a0\u3067\u3059\u3002\u5148\u306b\u52a0\u3048\u305f node \u30bf\u30b0\u306b\u3001\u65b0\u305f\u306b\u30bf\u30b0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">&lt;?php\r\n$xmltext = &lt;&lt;&lt; end_of_quote\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;root&gt;\r\n&lt;\/root&gt;\r\nend_of_quote;\r\n$xml = new DOMDocument();\r\n\/\/$xml-&gt;formatOutput = true;\r\n$xml-&gt;loadXML($xmltext);\r\n$root = $xml-&gt;getElementsByTagName(&quot;root&quot;)-&gt;item(0);\r\n$f = $xml-&gt;createDocumentFragment();\r\n$f-&gt;appendXML(&quot;&lt;node&gt;\\n&lt;a&gt;hello&lt;\/a&gt;\\n&lt;\/node&gt;&quot;);\r\n$h = $xml-&gt;documentElement-&gt;appendChild($f);\r\n$g = new DOMText(&quot;\\n\\n&quot;);\r\n$root-&gt;appendChild($g);\r\n$newnode = new DOMElement('b');\r\n$h-&gt;appendChild($newnode);\r\n$newnode-&gt;appendChild(new DOMText('hello'));\r\n$h-&gt;appendChild(new DOMText(&quot;\\n&quot;));\r\necho $xml-&gt;saveXML();\r\n?&gt;\r\n<\/code><\/pre>\n<p>\u3053\u306e\u51fa\u529b\u306f\u4e0b\u8a18\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;root&gt;\r\n&lt;node&gt;\r\n&lt;a&gt;hello&lt;\/a&gt;\r\n&lt;b&gt;hello&lt;\/b&gt;\r\n&lt;\/node&gt;\r\n&lt;\/root&gt; \r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u4ee5\u4e0b\u3001\u3082\u3046\u5c11\u3057\u7d9a\u3051\u307e\u3059\u3002\u5b50\u8981\u7d20\u3092\u53d6\u5f97\u3057\u3066\u3001\u305d\u306e\u5185\u5bb9\u3092\u8aad\u307f\u53d6\u3063\u3066\u307f\u307e\u3057\u305f\u3002\u6539\u884c\u306a\u3069\u3082\u5b50\u8981\u7d20\u3068\u306a\u308a\u307e\u3059\u3002if\u6587\u3092\u66f8\u3044\u3066\u3001\u6539\u884c\u306a\u3069\u3092\u62fe\u308f\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">&lt;?php\r\n$xmltext = &lt;&lt;&lt; end_of_quote\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;root&gt;\r\n&lt;node&gt;\r\na_hello\r\n&lt;node&gt;b_hello&lt;\/node&gt;\r\n&lt;\/node&gt;\r\n&lt;node&gt;\r\nc_hello\r\n&lt;\/node&gt;\r\n&lt;\/root&gt;\r\nend_of_quote;\r\n\r\n$xml = new DOMDocument();\r\n$xml-&gt;loadXML($xmltext);\r\n$root = $xml-&gt;getElementsByTagName(&quot;root&quot;)-&gt;item(0);\r\n$alist = $root-&gt;childNodes;\r\n\r\nforeach ($alist as $n) {\r\n     if (trim($n-&gt;nodeValue)) {\r\n          echo trim($n-&gt;nodeValue).&quot;\\n&quot;;\r\n     }\r\n}\r\n?&gt; \r\n<\/code><\/pre>\n<p>\u3053\u306e\u51fa\u529b\u304c\u4e0b\u8a18\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">a_hello\r\nb_hello\r\nc_hello\r\n<\/code><\/pre>\n<p>\u76f4\u63a5\u306e\u5b50\u8981\u7d20\uff08\u5b6b\u3067\u306a\u3044\uff09\u306e\u307f\u3092\u8aad\u307f\u53d6\u308a\u305f\u3044\u306e\u3067\u3059\u304c\u3001\u3084\u308a\u65b9\u304c\u308f\u304b\u3089\u306a\u3044\u3002<\/p>\n<p>\u6b21\u306b\u3001\u5b50\u8981\u7d20\u306e\u524a\u9664\u3067\u3059\u3002\u5165\u308c\u5b50\u306b\u306a\u3063\u305f node \u30bf\u30b0\u304b\u3089\u3001\u5916\u5074\u306e\u30bf\u30b0\u3060\u3051\u6b8b\u3057\u307e\u3059\u3002\u5185\u5074\u306b node \u30bf\u30b0\u3092\u6301\u3064\u304b\u3069\u3046\u304b\u3092\u8abf\u67fb\u3057\u3066\u3001\u6301\u3063\u3066\u3044\u308c\u3070\u524a\u9664\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">&lt;?php\r\n$xmltext = &lt;&lt;&lt; end_of_quote\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;root&gt;\r\n&lt;node&gt;\r\na_hello\r\n&lt;node&gt;b_hello&lt;\/node&gt;\r\n&lt;\/node&gt;\r\n&lt;node&gt;\r\nc_hello\r\n&lt;\/node&gt;\r\n&lt;\/root&gt;\r\nend_of_quote;\r\n\r\n$xml = new DOMDocument();\r\n$xml-&gt;loadXML($xmltext);\r\n$root = $xml-&gt;getElementsByTagName(&quot;root&quot;)-&gt;item(0);\r\n$alist = $root-&gt;childNodes;\r\n\r\nforeach ($alist as $n) {\r\n     if ($n-&gt;hasChildNodes()) {\r\n          $targetnode = $n-&gt;getElementsByTagName(&quot;node&quot;)-&gt;item(0);\r\n          if ($targetnode) {\r\n               $n-&gt;removeChild($targetnode);\r\n          }\r\n     }\r\n}\r\n\r\necho $xml-&gt;saveXML();\r\n?&gt; \r\n<\/code><\/pre>\n<p>\u51fa\u529b\u306f\u4e0b\u8a18\u3002<\/p>\n<pre><code style=\"color:midnightblue;\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;root&gt;\r\n&lt;node&gt;\r\na_hello\r\n&lt;\/node&gt;\r\n&lt;node&gt;\r\nc_hello\r\n&lt;\/node&gt;\r\n&lt;\/root&gt; \r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30bf\u30a4\u30c8\u30eb\u304c\u9069\u5f53\u306a\u306e\u304b\u3069\u3046\u304b\u5206\u304b\u308a\u307e\u305b\u3093\u304c\u3001SimpleXML \u3067\u306f\u6271\u3044\u306b\u304f\u3044\u51e6\u7406 &#8230; <a class=\"more-link\" href=\"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/?p=3988\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-3988","post","type-post","status-publish","format-standard","hentry","category-column"],"_links":{"self":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3988"}],"version-history":[{"count":12,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3988\/revisions"}],"predecessor-version":[{"id":5415,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3988\/revisions\/5415"}],"wp:attachment":[{"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3988"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zairyo.susi.oita-u.ac.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}