{"id":6097,"date":"2024-03-14T03:50:53","date_gmt":"2024-03-14T03:50:53","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/"},"modified":"2025-08-01T23:23:53","modified_gmt":"2025-08-01T23:23:53","slug":"how-does-the-xml-file-run-in-java","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/","title":{"rendered":"Parse XML in Java: DOM, SAX, JAXB"},"content":{"rendered":"<p>XML files in Java are not executed directly, but instead they are read and parsed using Java code. Common methods include using APIs such as DOM, SAX, or JAXB to manipulate XML files.<\/p>\n<ol>\n<li>Using DOM: DOM (Document Object Model) is an API based on a tree structure, which loads the entire XML document into memory and allows for manipulating the XML file through nodes. Parsing XML files with DOM requires loading the file into memory first, and then traversing and manipulating it through nodes.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">DocumentBuilderFactory<\/span> <span class=\"hljs-variable\">factory<\/span> <span class=\"hljs-operator\">=<\/span> DocumentBuilderFactory.newInstance();\r\n<span class=\"hljs-type\">DocumentBuilder<\/span> <span class=\"hljs-variable\">builder<\/span> <span class=\"hljs-operator\">=<\/span> factory.newDocumentBuilder();\r\n<span class=\"hljs-type\">Document<\/span> <span class=\"hljs-variable\">document<\/span> <span class=\"hljs-operator\">=<\/span> builder.parse(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">File<\/span>(<span class=\"hljs-string\">\"file.xml\"<\/span>));\r\n\r\n<span class=\"hljs-type\">Element<\/span> <span class=\"hljs-variable\">root<\/span> <span class=\"hljs-operator\">=<\/span> document.getDocumentElement();\r\n<span class=\"hljs-type\">NodeList<\/span> <span class=\"hljs-variable\">nodeList<\/span> <span class=\"hljs-operator\">=<\/span> root.getElementsByTagName(<span class=\"hljs-string\">\"tag\"<\/span>);\r\n<span class=\"hljs-keyword\">for<\/span>(<span class=\"hljs-type\">int<\/span> i=<span class=\"hljs-number\">0<\/span>; i&lt;nodeList.getLength(); i++){\r\n    <span class=\"hljs-type\">Node<\/span> <span class=\"hljs-variable\">node<\/span> <span class=\"hljs-operator\">=<\/span> nodeList.item(i);\r\n    <span class=\"hljs-comment\">\/\/\u5904\u7406\u8282\u70b9\u64cd\u4f5c<\/span>\r\n}\r\n<\/code><\/pre>\n<ol>\n<li>Using SAX: SAX (Simple API for XML) is an event-based API that parses XML files in a way that is driven by events. As the parser reads the XML file, it triggers corresponding events that developers can implement logic to handle.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">SAXParserFactory<\/span> <span class=\"hljs-variable\">factory<\/span> <span class=\"hljs-operator\">=<\/span> SAXParserFactory.newInstance();\r\n<span class=\"hljs-type\">SAXParser<\/span> <span class=\"hljs-variable\">parser<\/span> <span class=\"hljs-operator\">=<\/span> factory.newSAXParser();\r\n<span class=\"hljs-type\">DefaultHandler<\/span> <span class=\"hljs-variable\">handler<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">DefaultHandler<\/span>(){\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">startElement<\/span><span class=\"hljs-params\">(String uri, String localName, String qName, Attributes attributes)<\/span> <span class=\"hljs-keyword\">throws<\/span> SAXException {\r\n        <span class=\"hljs-comment\">\/\/\u5904\u7406\u5f00\u59cb\u6807\u7b7e\u4e8b\u4ef6<\/span>\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">characters<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">char<\/span>[] ch, <span class=\"hljs-type\">int<\/span> start, <span class=\"hljs-type\">int<\/span> length)<\/span> <span class=\"hljs-keyword\">throws<\/span> SAXException {\r\n        <span class=\"hljs-comment\">\/\/\u5904\u7406\u5143\u7d20\u5185\u5bb9\u4e8b\u4ef6<\/span>\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">endElement<\/span><span class=\"hljs-params\">(String uri, String localName, String qName)<\/span> <span class=\"hljs-keyword\">throws<\/span> SAXException {\r\n        <span class=\"hljs-comment\">\/\/\u5904\u7406\u7ed3\u675f\u6807\u7b7e\u4e8b\u4ef6<\/span>\r\n    }\r\n};\r\nparser.parse(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">File<\/span>(<span class=\"hljs-string\">\"file.xml\"<\/span>), handler);\r\n<\/code><\/pre>\n<ol>\n<li>JAXB, which stands for Java Architecture for XML Binding, is a part of the Java EE platform that allows for mapping between Java objects and XML documents. With JAXB, data from XML files can be mapped to Java objects and vice versa.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">JAXBContext<\/span> <span class=\"hljs-variable\">context<\/span> <span class=\"hljs-operator\">=<\/span> JAXBContext.newInstance(Class.class);\r\n<span class=\"hljs-type\">Unmarshaller<\/span> <span class=\"hljs-variable\">unmarshaller<\/span> <span class=\"hljs-operator\">=<\/span> context.createUnmarshaller();\r\n<span class=\"hljs-type\">Class<\/span> <span class=\"hljs-variable\">obj<\/span> <span class=\"hljs-operator\">=<\/span> (Class) unmarshaller.unmarshal(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">File<\/span>(<span class=\"hljs-string\">\"file.xml\"<\/span>));\r\n<\/code><\/pre>\n<p>With the above methods, it is possible to manipulate XML files in Java, read the data within them, and perform the appropriate processing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>XML files in Java are not executed directly, but instead they are read and parsed using Java code. Common methods include using APIs such as DOM, SAX, or JAXB to manipulate XML files. Using DOM: DOM (Document Object Model) is an API based on a tree structure, which loads the entire XML document into memory [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[1916,87,4033,7149,188],"class_list":["post-6097","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-dom","tag-java","tag-jaxb","tag-sax","tag-xml-parsing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Parse XML in Java: DOM, SAX, JAXB - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to parse XML files in Java using DOM, SAX, and JAXB APIs. Choose the right method for your project.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Parse XML in Java: DOM, SAX, JAXB\" \/>\n<meta property=\"og:description\" content=\"Learn how to parse XML files in Java using DOM, SAX, and JAXB APIs. Choose the right method for your project.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T03:50:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-01T23:23:53+00:00\" \/>\n<meta name=\"author\" content=\"William Carter\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"William Carter\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/\"},\"author\":{\"name\":\"William Carter\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\"},\"headline\":\"Parse XML in Java: DOM, SAX, JAXB\",\"datePublished\":\"2024-03-14T03:50:53+00:00\",\"dateModified\":\"2025-08-01T23:23:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/\"},\"wordCount\":197,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"DOM\",\"Java\",\"JAXB\",\"SAX\",\"XML parsing\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/\",\"name\":\"Parse XML in Java: DOM, SAX, JAXB - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T03:50:53+00:00\",\"dateModified\":\"2025-08-01T23:23:53+00:00\",\"description\":\"Learn how to parse XML files in Java using DOM, SAX, and JAXB APIs. Choose the right method for your project.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Parse XML in Java: DOM, SAX, JAXB\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0\",\"name\":\"William Carter\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g\",\"caption\":\"William Carter\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Parse XML in Java: DOM, SAX, JAXB - Blog - Silicon Cloud","description":"Learn how to parse XML files in Java using DOM, SAX, and JAXB APIs. Choose the right method for your project.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Parse XML in Java: DOM, SAX, JAXB","og_description":"Learn how to parse XML files in Java using DOM, SAX, and JAXB APIs. Choose the right method for your project.","og_url":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T03:50:53+00:00","article_modified_time":"2025-08-01T23:23:53+00:00","author":"William Carter","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"William Carter","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/"},"author":{"name":"William Carter","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0"},"headline":"Parse XML in Java: DOM, SAX, JAXB","datePublished":"2024-03-14T03:50:53+00:00","dateModified":"2025-08-01T23:23:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/"},"wordCount":197,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["DOM","Java","JAXB","SAX","XML parsing"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/","url":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/","name":"Parse XML in Java: DOM, SAX, JAXB - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T03:50:53+00:00","dateModified":"2025-08-01T23:23:53+00:00","description":"Learn how to parse XML files in Java using DOM, SAX, and JAXB APIs. Choose the right method for your project.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-does-the-xml-file-run-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Parse XML in Java: DOM, SAX, JAXB"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/f697031891aacefc4b681d139781d3c0","name":"William Carter","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1786698071dd8d74bec894b512f9e3c610c3a2a32985f67e688976cee3c8bbef?s=96&d=mm&r=g","caption":"William Carter"},"url":"https:\/\/www.silicloud.com\/blog\/author\/williamcarter\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/6097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=6097"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/6097\/revisions"}],"predecessor-version":[{"id":150858,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/6097\/revisions\/150858"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=6097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=6097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=6097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}