json的org.w3c.dom.Node的实现 [英] Implementations of org.w3c.dom.Node for json

查看:301
本文介绍了json的org.w3c.dom.Node的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找org.w3c.dom.Node的实现,用于除XML之外的数据类型,例如json或avro.这将允许我重用针对org.w3c.dom.Node编写的功能,例如xpath.

I am looking for an implementation of org.w3c.dom.Node for data types other than XML, like json or avro. This will allow me to reuse functionalities written against org.w3c.dom.Node, such as xpath.

org.w3c.dom.Node document = new JsonDocument(myJsonMessage);

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

String msg = xpath.evaluate("/document/element", document);

msg是针对json文档对xpath的评估,前提是我为Json实现了Node/Document.

msg is an evaluation of xpath against a json document, provided I have an implementation of Node/Document for Json.

推荐答案

org.w3c.dom.Node是一个接口.我最终为json,avro,bson(mongodb)编写了接口的实现.它工作得很好.通过执行此操作,我们可以对json,avro,bson等使用xpath,xslt.

org.w3c.dom.Node is an interface. I ended up writing implementations of the interface for json, avro, bson (mongodb). It works perfectly fine. We can use xpath, xslt against json, avro, bson, etc by doing this.

您唯一需要记住的是某些文档类型比其他文档类型更丰富". xml具有节点的值,而其他大多数格式则没有.

The only thing you have to remember is that some document types are "richer" than others. xml has a value for a node, where most other formats don't.

例如:

<MyDocument>
  <MyChild1>Child1Value</MyChild1>
  <MyChild2>Child2Value</MyChild2>
  <MyChild3 attribute1="Child3 attribute1" attribute2="Child3 attribute2"></MyChild3>
</MyDocument>

要在json中表示同一文档,您需要引入一个约定.在下面的示例中,约定是对节点值使用键"_rootValue":

To represent the same document in json, you need to introduce a convention. In the below example the convention is to use the key "_rootValue" for node value:

  {
    "MyDocument": {
      "MyChild1": {
        "_rootValue": "Child1Value"
      },
      "MyChild2": {
        "_rootValue": "Child2Value"
      },
      "MyChild3": {
        "attribute1": "Child3 attribute1",
        "attribute2": "Child3 attribute2"
      },
    }
  }

这篇关于json的org.w3c.dom.Node的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆