Đôi khi khi lập trình bằng Java, chúng ta nhận được một chuỗi (String) thực chất là một XML và để xử lý nó, chúng ta cần chuyển đổi nó sang XML Document (org.w3c.dom.Document). Ngoài ra, với mục đích debug hoặc gửi đến một hàm khác, chúng ta có thể cần chuyển đổi đối tượng Document sang String.
Bài viết này sẽ hướng dẫn bạn cách chuyển đổichuỗi thành XML và XML sang chuỗi trong Java.
Chuyển đổi chuỗi thành XML
Dưới đây tôi đề cập đến hai hàm tiện ích.
Document convertStringToDocument(String xmlStr): Phương thức này sẽ nhận đầu vào là String và sau đó chuyển đổi nó sang DOM Document và trả về. Chúng ta sẽ sử dụng InputSource và StringReader cho việc chuyển đổi này.
String convertDocumentToString(Document doc): Phương thức này sẽ nhận đầu vào là Document và chuyển đổi nó sang String. Chúng ta sẽ sử dụng Transformer, StringWriter và StreamResult cho việc chuyển đổi này.
Khi chúng ta chạy chương trình trên, chúng ta sẽ nhận được cùng một đầu ra String mà chúng ta đã sử dụng để tạo DOM Document.
Pankaj25DeveloperMale
Bạn có thể sử dụng replaceAll("\\n|\\r", "") để loại bỏ các ký tự xuống dòng khỏi String và đưa nó về định dạng nhỏ gọn.
Về tác giả
Bao TranWeb Developer
I’m passionate about web development and sharing my insights through articles, with over 8 years of experience. I hope these sharings inspire you and help build a strong web development community.
@#@
Tôi đam mê phát triển web và chia sẻ những hiểu biết của mình thông qua các bài viết, với hơn 8 năm kinh nghiệm. Tôi hy vọng những chia sẻ này sẽ truyền cảm hứng cho các bạn và giúp xây dựng một cộng đồng phát triển web mạnh mẽ.
Cập nhật thông tin mới nhấtNhận các thông tin mới nhất về mối đe dọa, báo cáo an ninh mạng từ CyStack về hòm thư điện tử của bạn
Thảo luận (0)
Đăng nhập để thảo luận
Bài viết liên quan
{"success":true,"head":"<title>Hướng dẫn chuyển đổi chuỗi thành XML trong Java - CyStack Tutorial</title>\n<meta name=\"description\" content=\"Tìm hiểu cách chuyển đổi chuỗi thành XML dễ dàng với ví dụ minh họa, giúp bạn xử lý dữ liệu hiệu quả trong lập trình.\"/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large\"/>\n<link rel=\"canonical\" href=\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/\" />\n<meta property=\"og:locale\" content=\"en_US\" />\n<meta property=\"og:type\" content=\"article\" />\n<meta property=\"og:title\" content=\"Hướng dẫn chuyển đổi chuỗi thành XML trong Java - CyStack Tutorial\" />\n<meta property=\"og:description\" content=\"Tìm hiểu cách chuyển đổi chuỗi thành XML dễ dàng với ví dụ minh họa, giúp bạn xử lý dữ liệu hiệu quả trong lập trình.\" />\n<meta property=\"og:url\" content=\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/\" />\n<meta property=\"og:site_name\" content=\"CyStack Tutorial\" />\n<meta property=\"article:tag\" content=\"vi\" />\n<meta property=\"article:section\" content=\"Java\" />\n<meta property=\"og:image\" content=\"https://s2.cystack.net/tutorial/19073853/chuyen-doi-chuoi-thanh-xml.jpg\" />\n<meta property=\"og:image:secure_url\" content=\"https://s2.cystack.net/tutorial/19073853/chuyen-doi-chuoi-thanh-xml.jpg\" />\n<meta property=\"og:image:width\" content=\"1200\" />\n<meta property=\"og:image:height\" content=\"630\" />\n<meta property=\"og:image:alt\" content=\"chuyển đổi chuỗi thành XML\" />\n<meta property=\"og:image:type\" content=\"image/jpeg\" />\n<meta property=\"article:published_time\" content=\"2025-08-19T07:40:12+07:00\" />\n<meta name=\"twitter:card\" content=\"summary_large_image\" />\n<meta name=\"twitter:title\" content=\"Hướng dẫn chuyển đổi chuỗi thành XML trong Java - CyStack Tutorial\" />\n<meta name=\"twitter:description\" content=\"Tìm hiểu cách chuyển đổi chuỗi thành XML dễ dàng với ví dụ minh họa, giúp bạn xử lý dữ liệu hiệu quả trong lập trình.\" />\n<meta name=\"twitter:image\" content=\"https://s2.cystack.net/tutorial/19073853/chuyen-doi-chuoi-thanh-xml.jpg\" />\n<meta name=\"twitter:label1\" content=\"Written by\" />\n<meta name=\"twitter:data1\" content=\"Bao Tran\" />\n<meta name=\"twitter:label2\" content=\"Time to read\" />\n<meta name=\"twitter:data2\" content=\"2 minutes\" />\n<script type=\"application/ld+json\" class=\"rank-math-schema\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https://blog.cystack.org/tutorial/#person\",\"name\":\"CyStack Tutorial\"},{\"@type\":\"WebSite\",\"@id\":\"https://blog.cystack.org/tutorial/#website\",\"url\":\"https://blog.cystack.org/tutorial\",\"name\":\"CyStack Tutorial\",\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https://s2.cystack.net/tutorial/19073853/chuyen-doi-chuoi-thanh-xml.jpg\",\"url\":\"https://s2.cystack.net/tutorial/19073853/chuyen-doi-chuoi-thanh-xml.jpg\",\"width\":\"1200\",\"height\":\"630\",\"caption\":\"chuy\\u1ec3n \\u0111\\u1ed5i chu\\u1ed7i th\\u00e0nh XML\",\"inLanguage\":\"en-US\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":\"1\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":\"2\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/category/java/\",\"name\":\"Java\"}},{\"@type\":\"ListItem\",\"position\":\"3\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/\",\"name\":\"H\\u01b0\\u1edbng d\\u1eabn chuy\\u1ec3n \\u0111\\u1ed5i chu\\u1ed7i th\\u00e0nh XML trong Java\"}}]},{\"@type\":\"WebPage\",\"@id\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/#webpage\",\"url\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/\",\"name\":\"H\\u01b0\\u1edbng d\\u1eabn chuy\\u1ec3n \\u0111\\u1ed5i chu\\u1ed7i th\\u00e0nh XML trong Java - CyStack Tutorial\",\"datePublished\":\"2025-08-19T07:40:12+07:00\",\"dateModified\":\"2025-08-19T07:40:12+07:00\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/#website\"},\"primaryImageOfPage\":{\"@id\":\"https://s2.cystack.net/tutorial/19073853/chuyen-doi-chuoi-thanh-xml.jpg\"},\"inLanguage\":\"en-US\",\"breadcrumb\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/#breadcrumb\"}},{\"@type\":\"Person\",\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\",\"url\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"url\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"caption\":\"Bao Tran\",\"inLanguage\":\"en-US\"}},{\"@type\":\"BlogPosting\",\"headline\":\"H\\u01b0\\u1edbng d\\u1eabn chuy\\u1ec3n \\u0111\\u1ed5i chu\\u1ed7i th\\u00e0nh XML trong Java - CyStack Tutorial\",\"keywords\":\"chuy\\u1ec3n \\u0111\\u1ed5i chu\\u1ed7i th\\u00e0nh XML\",\"datePublished\":\"2025-08-19T07:40:12+07:00\",\"dateModified\":\"2025-08-19T07:40:12+07:00\",\"articleSection\":\"Java\",\"author\":{\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\"},\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"description\":\"T\\u00ecm hi\\u1ec3u c\\u00e1ch chuy\\u1ec3n \\u0111\\u1ed5i chu\\u1ed7i th\\u00e0nh XML d\\u1ec5 d\\u00e0ng v\\u1edbi v\\u00ed d\\u1ee5 minh h\\u1ecda, gi\\u00fap b\\u1ea1n x\\u1eed l\\u00fd d\\u1eef li\\u1ec7u hi\\u1ec7u qu\\u1ea3 trong l\\u1eadp tr\\u00ecnh.\",\"name\":\"H\\u01b0\\u1edbng d\\u1eabn chuy\\u1ec3n \\u0111\\u1ed5i chu\\u1ed7i th\\u00e0nh XML trong Java - CyStack Tutorial\",\"@id\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/#richSnippet\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/#webpage\"},\"image\":{\"@id\":\"https://s2.cystack.net/tutorial/19073853/chuyen-doi-chuoi-thanh-xml.jpg\"},\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/19/chuyen-doi-chuoi-thanh-xml/#webpage\"}}]}</script>\n"}