{"record":{"id":"5b5d95a1e54c20eb","repo":"alibaba/spring-cloud-alibaba","slug":"the-xml-content-parse-error","errorCode":null,"errorMessage":"The xml content parse error.","messagePattern":"The xml content parse error\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"spring-cloud-alibaba-starters/spring-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosXmlPropertySourceLoader.java","lineNumber":113,"sourceCode":"\t\t}\n\t\treturn Collections.singletonList(\n\t\t\t\tnew OriginTrackedMapPropertySource(name, nacosDataMap, true));\n\n\t}\n\n\tprivate @Nullable Map<String, Object> parseXml2Map(Resource resource) throws IOException {\n\t\tMap<String, Object> map = new LinkedHashMap<>(32);\n\t\ttry {\n\t\t\tDocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()\n\t\t\t\t\t.newDocumentBuilder();\n\t\t\tDocument document = documentBuilder.parse(resource.getInputStream());\n\t\t\tif (null == document) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tparseNodeList(document.getChildNodes(), map, \"\");\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new IOException(\"The xml content parse error.\", e.getCause());\n\t\t}\n\t\treturn map;\n\t}\n\n\tprivate void parseNodeList(NodeList nodeList, Map<String, Object> map,\n\t\t\tString parentKey) {\n\t\tif (nodeList == null || nodeList.getLength() < 1) {\n\t\t\treturn;\n\t\t}\n\t\tparentKey = parentKey == null ? \"\" : parentKey;\n\t\tfor (int i = 0; i < nodeList.getLength(); i++) {\n\t\t\tNode node = nodeList.item(i);\n\t\t\tString value = node.getNodeValue();\n\t\t\tvalue = value == null ? \"\" : value.trim();\n\t\t\tString name = node.getNodeName();\n\t\t\tname = name == null ? \"\" : name.trim();\n\n\t\t\tif (StringUtils.isEmpty(name)) {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/alibaba/spring-cloud-alibaba/blob/115d5901102009492e05d5ec18c3f79cad4077d0/spring-cloud-alibaba-starters/spring-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/parser/NacosXmlPropertySourceLoader.java#L95-L131","documentation":"Thrown by NacosXmlPropertySourceParser.parseXml2Map when DocumentBuilder.parse(resource.getInputStream()) or the subsequent node walk raises any Exception. The catch-all wraps it as IOException(\"The xml content parse error.\") carrying e.getCause(). This loader is used when a Nacos config file has the .xml extension; the content must be well-formed XML parseable by the default DocumentBuilder.","triggerScenarios":"A Nacos config published with an .xml dataId whose content is not well-formed XML — unclosed tags, invalid nesting, BOM/encoding issues, stray characters, a DOCTYPE/entity reference the default factory rejects, or non-XML text accidentally saved under an xml dataId.","commonSituations":"Editing config in Nacos console and saving malformed XML; pasting a properties/yaml snippet into an xml dataId; encoding/BOM from an editor; XXE-guard changes in the JDK tightening DocumentBuilderFactory defaults.","solutions":["Validate the published XML is well-formed (open it in a browser or xmllint --noout file.xml) and fix structural errors.","Ensure the dataId extension matches the actual content; move non-XML content to a .properties or .yaml dataId.","Remove any DOCTYPE declarations or unsupported entity references that the default DocumentBuilder disallows.","Re-save with UTF-8 (no BOM) encoding."],"exampleFix":"<!-- before (malformed) -->\n<config>\n  <key>value\n</config>\n<!-- after -->\n<config>\n  <key>value</key>\n</config>","handlingStrategy":"try-catch","validationCode":"// Before publishing/binding, validate the XML is well-formed.\nDocumentBuilderFactory f = DocumentBuilderFactory.newInstance();\nDocumentBuilder b = f.newDocumentBuilder();\nb.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));","typeGuard":"static boolean isWellFormedXml(String xml) {\n    try {\n        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();\n        f.newDocumentBuilder().parse(new java.io.ByteArrayInputStream(xml.getBytes(java.nio.charset.StandardCharsets.UTF_8)));\n        return true;\n    } catch (Exception e) {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    // load the xml config\n} catch (IOException e) {\n    if (\"The xml content parse error.\".equals(e.getMessage())) {\n        // re-validate the published XML in the Nacos console and fix structure\n    }\n}","preventionTips":["Validate XML in the Nacos console before publishing (well-formedness).","Match the dataId extension to the content type.","Save as UTF-8 without BOM and avoid unsupported DOCTYPE/entity declarations."],"tags":["nacos","config","xml","parsing","documentbuilder"],"backgroundTag":null,"analyzedSha":"115d5901102009492e05d5ec18c3f79cad4077d0","analyzedAt":"2026-08-14T04:47:13.900Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}