简体   繁体   English

JSON 使用 StAXON 解析 - XMLStreamException 当元素名称中有一个字符时

[英]JSON parsing using StAXON - XMLStreamException when there is a : character in element name

We are using StAXON (due to its performance via StAX processing) to parse json input which contains colon(:) as element name.我们正在使用 StAXON(由于其通过 StAX 处理的性能)来解析包含冒号(:)作为元素名称的 json 输入。 Colon is valid element name in JSON but while parsing via StAXON we are getting exception (XMLStreamException).冒号是 JSON 中的有效元素名称,但在通过 StAXON 进行解析时,我们遇到异常 (XMLStreamException)。

It works if I use namespaceSeparator as '\0', like new JsonXMLConfigBuilder().namespaceSeparator(Character.MIN_VALUE) .如果我将 namespaceSeparator 用作“\0”,它会起作用,例如new JsonXMLConfigBuilder().namespaceSeparator(Character.MIN_VALUE) But this just changes the namespace separator.但这只是改变了命名空间分隔符。

My Question/Doubt:我的问题/疑问:

  1. Is it safe to do this?这样做安全吗? Are there chances of getting issue(s)是否有可能出现问题
  2. Is there a way to ignore colon while parsing (reading/writing) JSON?有没有办法在解析(读/写)JSON 时忽略冒号?
  3. Is there any other alternative to StAXON to parse JSON based on events like StAX parsing is done for XML.是否有任何其他替代 StAXON 的方法来根据事件解析 JSON,例如为 XML 完成 StAX 解析。

Sample Input : {"cust:person":{"name":"John Doe","phone":"555-1111"}}示例输入{"cust:person":{"name":"John Doe","phone":"555-1111"}}

Exception例外

Exception in thread "main" javax.xml.stream.XMLStreamException: Unbound prefix: cust
at de.odysseus.staxon.base.AbstractXMLStreamScope.verify(AbstractXMLStreamScope.java:212)
at de.odysseus.staxon.base.AbstractXMLStreamScope.setStartTagClosed(AbstractXMLStreamScope.java:234)
at de.odysseus.staxon.base.AbstractXMLStreamReader.ensureStartTagClosed(AbstractXMLStreamReader.java:191)
at de.odysseus.staxon.base.AbstractXMLStreamReader.readStartElementTag(AbstractXMLStreamReader.java:272)
at de.odysseus.staxon.json.JsonXMLStreamReader.readStartElementTag(JsonXMLStreamReader.java:93)
at de.odysseus.staxon.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:202)
at de.odysseus.staxon.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:153)
at de.odysseus.staxon.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:183)
at de.odysseus.staxon.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:153)
at de.odysseus.staxon.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:183)
at de.odysseus.staxon.base.AbstractXMLStreamReader.initialize(AbstractXMLStreamReader.java:216)
at de.odysseus.staxon.json.JsonXMLStreamReader.initialize(JsonXMLStreamReader.java:87)
at de.odysseus.staxon.json.JsonXMLStreamReader.(JsonXMLStreamReader.java:78)
at de.odysseus.staxon.json.JsonXMLInputFactory.createXMLStreamReader(JsonXMLInputFactory.java:150)
at de.odysseus.staxon.json.JsonXMLInputFactory.createXMLStreamReader(JsonXMLInputFactory.java:45)
at JsonXml.main(JsonXml.java:27)

Thanks谢谢

Colon is a legal name character in case the parser is not namespace aware.冒号是一个合法的名称字符,以防解析器无法识别名称空间。 You should be able to turn off namespace awareness on the underlying XMLInputFactory (don't know the details of StAXON).您应该能够关闭底层 XMLInputFactory 上的名称空间感知(不知道 StAXON 的详细信息)。

It's safe to do this but it's probably not what you want.这样做是安全的,但这可能不是您想要的。 Since StAXON tries to treat JSON like XML, you should give the library what it wants: A namespace for the „cust“ prefix.由于 StAXON 试图像 XML 一样对待 JSON,你应该给库它想要的东西:“cust”前缀的命名空间。 Read about StAXON's conventions here .在此处阅读 StAXON 的约定

To declare a namespace:声明一个命名空间:

new JsonXMLConfigBuilder().namespaceMapping(„cust“, „http://example.com/ns/cust“)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM