【问题标题】:Escape a forward slash in attribute name of XML在 XML 的属性名称中转义正斜杠
【发布时间】:2017-10-18 19:31:02
【问题描述】:

我在操作之前将 XML 字符串转换为文档。我从第三方 API 接收 XML。

这是我的 XML

<Student full/part="part" name="Some Name" dob="2000-08-01" totalMarks="565" science="9" maths="8" registration="987654"  class="distinction" />

这是我得到的例外

org.apache.camel.TypeConversionException: Error during type conversion from type: java.lang.String to the required type: org.w3c.dom.Document with value  due org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 64; Attribute name "full" associated with an element type "Student" must be followed by the ' = ' character.

现在我没有定义,我只是从这个 XML 中检索属性值。即明天这个元素类型可能是来自学生的老师,但我只是将注册、班级甚至“完整/部分”等属性的值放在一起。

我想知道哪个 org.w3c.dom.Document 允许在给定元素类型的这个 XML 属性名称中使用这个正斜杠。

【问题讨论】:

  • 这不是xml。你从哪里得到这个字符串?可能是打算成为String
  • @svasa 你的意思是任何元素的属性名称都不能有正斜杠吗??
  • @VirajNalawade:正确,属性名称中不允许使用正斜杠。有关详细信息,请参阅my answer。您可以改用-_,或者您可以考虑full="true" 或其他重命名可能性。
  • @kjhughes 谢谢 .. 即使我没有处理任何具有相似属性名称的 XML.. 但我对提供给我的 XML 几乎没有控制..
  • 您仍然没有抓住重点:提供给您的不是 XML。这是务实的。您不能使用 XML 库或工具来处理该文本数据。告诉提供商修复它或提供标准和工具的生态系统来解析和处理它(祝你好运)。

标签: java xml xml-parsing jaxb apache-camel


【解决方案1】:

XML attribute(或 XML element)的 name

[5] Name ::= NameStartChar (NameChar)*

不能包含/ 字符:

[4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

[4a] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

没有其他允许的转义机制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2012-04-29
    • 2015-01-23
    相关资源
    最近更新 更多