{"id":334,"date":"2025-09-09T04:49:02","date_gmt":"2025-09-09T04:49:02","guid":{"rendered":"https:\/\/iotnoob.com\/wordpress\/?p=334"},"modified":"2025-09-09T10:07:35","modified_gmt":"2025-09-09T10:07:35","slug":"convert-xml-to-object-in-java","status":"publish","type":"post","link":"https:\/\/iotnoob.com\/wordpress\/2025\/09\/09\/convert-xml-to-object-in-java\/","title":{"rendered":"\u0e41\u0e1b\u0e25\u0e07 XML \u0e01\u0e25\u0e31\u0e1a\u0e21\u0e32\u0e40\u0e1b\u0e47\u0e19 Object \u0e43\u0e19 Java"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u0e41\u0e1b\u0e25\u0e07 Object \u0e40\u0e1b\u0e47\u0e19 XML -> <a href=\"https:\/\/iotnoob.com\/wordpress\/2025\/08\/16\/create-xml-file-from-object-in-java\/\">https:\/\/iotnoob.com\/wordpress\/2025\/08\/16\/create-xml-file-from-object-in-java\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0e17\u0e35\u0e19\u0e35\u0e49 \u0e43\u0e19\u0e17\u0e32\u0e07\u0e01\u0e25\u0e31\u0e1a\u0e01\u0e31\u0e19 \u0e40\u0e23\u0e32\u0e01\u0e47\u0e41\u0e1b\u0e25\u0e07\u0e01\u0e25\u0e31\u0e1a\u0e44\u0e14\u0e49\u0e40\u0e2b\u0e21\u0e37\u0e2d\u0e19\u0e01\u0e31\u0e19<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e19\u0e35\u0e49\u0e08\u0e30\u0e22\u0e31\u0e07\u0e44\u0e21\u0e48\u0e43\u0e0a\u0e49 XSD \u0e40\u0e1e\u0e37\u0e48\u0e2d verify XML \u0e27\u0e48\u0e32 format \u0e16\u0e39\u0e01\u0e15\u0e49\u0e2d\u0e07\u0e2b\u0e23\u0e37\u0e2d\u0e44\u0e21\u0e48<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    private static void readAndVerifyXml() {\n        try {\n            \/\/ 1. \u0e2a\u0e23\u0e49\u0e32\u0e07 JAXB Context \u0e08\u0e32\u0e01 Class \u0e17\u0e35\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\n            JAXBContext context = JAXBContext.newInstance(StackRootCollection.class);\n\n            \/\/ 2. \u0e2a\u0e23\u0e49\u0e32\u0e07 Unmarshaller (\u0e15\u0e31\u0e27\u0e41\u0e1b\u0e25\u0e07 XML \u0e40\u0e1b\u0e47\u0e19 Object)\n            Unmarshaller unmarshaller = context.createUnmarshaller();\n\n            File xmlFile = new File(\"C:\\\\tmp\\\\resultxxx.stkx\");\n\n            \/\/ 6. \u0e17\u0e33\u0e01\u0e32\u0e23 Unmarshal (\u0e41\u0e1b\u0e25\u0e07\u0e44\u0e1f\u0e25\u0e4c XML \u0e40\u0e1b\u0e47\u0e19 Object)\n            \/\/ \u0e2b\u0e32\u0e01 XML \u0e44\u0e21\u0e48\u0e15\u0e23\u0e07\u0e15\u0e32\u0e21 XSD, JAXBException \u0e08\u0e30\u0e16\u0e39\u0e01\u0e42\u0e22\u0e19\u0e2d\u0e2d\u0e01\u0e21\u0e32\u0e15\u0e23\u0e07\u0e19\u0e35\u0e49\n            StackRootCollection customer = (StackRootCollection) unmarshaller.unmarshal(xmlFile);\n\n            \/\/ \u0e16\u0e49\u0e32\u0e2a\u0e33\u0e40\u0e23\u0e47\u0e08 \u0e41\u0e2a\u0e14\u0e07\u0e1c\u0e25\u0e25\u0e31\u0e1e\u0e18\u0e4c\n            System.out.println(\"\u2705 Success! XML is valid and parsed correctly.\");\n            System.out.println(\"Resulting object: \" + customer);\n\n        } catch (JAXBException e) {\n            \/\/ \u0e16\u0e49\u0e32\u0e44\u0e21\u0e48\u0e2a\u0e33\u0e40\u0e23\u0e47\u0e08 (\u0e40\u0e0a\u0e48\u0e19 XML \u0e1c\u0e34\u0e14\u0e42\u0e04\u0e23\u0e07\u0e2a\u0e23\u0e49\u0e32\u0e07)\n            System.out.println(\"\u274c Error! XML is invalid or does not match the XSD schema.\");\n            System.err.println(\"Error Details: \" + e.getMessage());\n        } catch (Exception e) {\n            \/\/ \u0e08\u0e31\u0e14\u0e01\u0e32\u0e23 Exception \u0e2d\u0e37\u0e48\u0e19\u0e46 \u0e17\u0e35\u0e48\u0e2d\u0e32\u0e08\u0e40\u0e01\u0e34\u0e14\u0e02\u0e36\u0e49\u0e19\n            System.err.println(\"An unexpected error occurred: \" + e.getMessage());\n        }\n    }<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"916\" height=\"595\" src=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/09\/image-1.png\" alt=\"\" class=\"wp-image-335\" srcset=\"https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/09\/image-1.png 916w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/09\/image-1-300x195.png 300w, https:\/\/iotnoob.com\/wordpress\/wp-content\/uploads\/2025\/09\/image-1-768x499.png 768w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u0e41\u0e1b\u0e25\u0e07 Object \u0e40\u0e1b\u0e47\u0e19 XML -> https:\/\/iotnoob.com\/wordpress\/2025\/08\/16\/create-xml-file-from-object-in-java\/ \u0e17\u0e35\u0e19\u0e35\u0e49 \u0e43\u0e19\u0e17\u0e32\u0e07\u0e01\u0e25\u0e31\u0e1a\u0e01\u0e31\u0e19 \u0e40\u0e23\u0e32\u0e01\u0e47\u0e41\u0e1b\u0e25\u0e07\u0e01\u0e25\u0e31\u0e1a\u0e44\u0e14\u0e49\u0e40\u0e2b\u0e21\u0e37\u0e2d\u0e19\u0e01\u0e31\u0e19 \u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e19\u0e35\u0e49\u0e08\u0e30\u0e22\u0e31\u0e07\u0e44\u0e21\u0e48\u0e43\u0e0a\u0e49 XSD \u0e40\u0e1e\u0e37\u0e48\u0e2d verify XML \u0e27\u0e48\u0e32&hellip;<\/p>\n","protected":false},"author":1,"featured_media":337,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,2],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-programming"],"_links":{"self":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":1,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":336,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/334\/revisions\/336"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media\/337"}],"wp:attachment":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}