Java 中的 XML 解析/Dom 操作

发布于 2024-08-23 13:40:07 字数 1494 浏览 4 评论 0原文

我试图弄清楚如何最好地将其翻译

<Source><properties>
  ....
  <name>wer</name>
  <delay>
    <type>Deterministic</type>
    <parameters length="1">
      <param value="78" type="Time"/>
    </parameters>
  </delay>
  <batchSize>
    <type>Cauchy</type>
    <parameters length="2">
      <param value="23" type="Alpha"/>
  <param value="7878" type="Beta"/>
    </parameters>
  </batchSize>
 ...
</properties></Source>

<Source><properties>
  ....
  <name>wer</name>
  <delay>
    <Deterministic Time="78"/>
  </delay>
  <batchSize>
      <Cauchy Alpha="23" Beta="7878"/>
  </batchSize>
 ........
</properties></Source>

:我尝试使用 DocumentBuilderFactory,但是虽然我可以访问名称标签的值,但无法访问延迟/批处理部分中的值。这是我使用的代码

Element prop = (Element)propertyNode;

NodeList nodeIDProperties = prop.getElementsByTagName("name");
Element nameElement = (Element)nodeIDProperties.item(0);

NodeList textFNList = nameElement.getChildNodes();
String nodeNameValue = ((org.w3c.dom.Node)textFNList.item(0)).getNodeValue().trim();

//--------
NodeList delayNode = prop.getElementsByTagName("delay");

调用 getElementByName("type") 或 "parameters" 似乎没有返回我可以使用的任何内容。我是否遗漏了什么,或者是否有更干净的方法来处理现有的 xml。

需要采用定义的格式以允许 Castor 进行编组和解组。

任何帮助将不胜感激。

I'm trying to figure out how best to translate this:

<Source><properties>
  ....
  <name>wer</name>
  <delay>
    <type>Deterministic</type>
    <parameters length="1">
      <param value="78" type="Time"/>
    </parameters>
  </delay>
  <batchSize>
    <type>Cauchy</type>
    <parameters length="2">
      <param value="23" type="Alpha"/>
  <param value="7878" type="Beta"/>
    </parameters>
  </batchSize>
 ...
</properties></Source>

Into:

<Source><properties>
  ....
  <name>wer</name>
  <delay>
    <Deterministic Time="78"/>
  </delay>
  <batchSize>
      <Cauchy Alpha="23" Beta="7878"/>
  </batchSize>
 ........
</properties></Source>

I've tried using DocumentBuilderFactory, but I while I can access the value of the name tag, I cannot access the values in the delay/batch section. This is code I used

Element prop = (Element)propertyNode;

NodeList nodeIDProperties = prop.getElementsByTagName("name");
Element nameElement = (Element)nodeIDProperties.item(0);

NodeList textFNList = nameElement.getChildNodes();
String nodeNameValue = ((org.w3c.dom.Node)textFNList.item(0)).getNodeValue().trim();

//--------
NodeList delayNode = prop.getElementsByTagName("delay");

Calling getElementByName("type") or "parameters" doesn't seem to return anything I can work with. Am I missing something, or is there a cleaner way to process the exisiting xml.

The need to be in the defined format to allow for marshalling and unmarshalling by Castor.

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白色秋天 2024-08-30 13:40:07

有多种方法可以转换 XML。

1) 您可以使用XSLT(XSL Transformations)来转换XML。它是一种基于 XML 的语言,用于将 XML 文档转换为其他 XML 文档、文本或 HTML。语法很难学。然而,它是 XML 转换的强大工具。 这里是一个教程。对于将 XSLT 与 Java 结合使用,我建议使用 Saxon,它还附带了很好的文档。使用 XSLT 的一大优点是转换可以在单独的模板中具体化。所以你的 Java 代码不会被翻译内容混淆。然而,正如前面提到的,学习曲线肯定更陡峭。

2)您可以使用XPath轻松选择节点。 XPath 是一种用于在 XML 文档中选择节点的查询语言。顺便说一下,XPath 也用于 XSLT。例如,XPath 查询

//delay[type = 'Deterministic']/parameters/param/@value

选择包含在节点 param 中的所有参数 value,这些参数是包含节点 type< 的 delay 的子节点。 /code> 值为“确定性”。 这里是一个用于测试 XPath 查询的不错的 Web 应用程序。 这里是一个如何在 Java 中使用 XPath 的教程,此处有关 XPath 的一般信息。您可以使用 XPath 表达式在 Java 代码中选择正确的节点。恕我直言,这比直接使用 DOM 对象模型更具可读性和可维护性(正如您已经了解到的那样,这有时也是很落后的)。

3) 您可以使用 Smooks 来执行 XML 转换。如果转换变得相当复杂,这尤其有用。 Smooks 从输入 XML 填充对象模型,并通过使用 Freemarker 或 XSL 模板的模板机制输出结果 XML。 Smooks 具有非常高的吞吐量,用于 ESB 等高性能环境(例如 JBoss ESB、Apache ServiceMix)。不过对于你的场景来说可能会被压倒。

4) 您可以使用 Freemarker 进行转换。我没有这方面的经验,但据我所知它的使用相当简单。请参阅文档的“声明式 XML 处理”部分(另请参阅“公开 XML 文档” 了解如何读取源 XML)。对我来说似乎相当简单。如果您尝试一下这种方法,我很想听听。

There are a variety of ways to convert the XML.

1) You can use XSLT (XSL Transformations) to transform the XML. It is a XML based language used to transform XML documents in other XML documents, text, or HTML. The syntax is hard to learn. However it is a powerful tool for XML conversion. Here is a tutorial. For using XSLT with Java I would recommend Saxon which also comes with a nice documentation. The big plus using XSLT is that the conversion can be externalized in a seperate template. So your Java code is not obfuscated by the translation stuff. However, as mentioned the learning curve is definitly steeper.

2) You can use XPath to select the nodes easily. XPath is a query language for selecting nodes in a XML document. XPath is also used in XSLT by the way. E.g. the XPath query

//delay[type = 'Deterministic']/parameters/param/@value

selects all parameters value which are contained in a node param which are a child of delay containing a node type with the value "Deterministic". Here is a nice web application for testing XPath queries. Here is a tutorial how to use XPath in Java and here about XPath in general. You can use XPath expressions to select the right nodes in your Java code. IMHO this is far more readable and maintainable than using the DOM object model directly (which is also from time to time ackward as you have already learned).

3) You can use Smooks for doing XML transformations. This is especially useful if the transformation gets rather complex. Smooks populates a object model from the input XML and outputs the result XML via a templating mechanism either using Freemarker or XSL templates. Smooks has a very high througput and is used in high performance environments like ESBs (e.g. JBoss ESB, Apache ServiceMix). Might be overpowered for yur scenario though.

4) You could use Freemarker to do the transformation. I have no experience in this, but as I heared it can be used fairly simple. See the "Declarative XML processing" section of the documentation (also take a look at "Exposing XML documents" to learn how to read the source XML). Seems fairly simple to me. If you try your luck with this approach, I would love to hear about it.

默嘫て 2024-08-30 13:40:07

这看起来像是 XPATH 或其他 XML 转换 API 的工作。

查看:http://www.ibm.com/developerworks/library/x -javaxpathapi.html

This looks like a job for XPATH or some other XML transformation API.

Check out: http://www.ibm.com/developerworks/library/x-javaxpathapi.html

看轻我的陪伴 2024-08-30 13:40:07

虽然 XSLT 可能是实现此目的的最佳方法,但如果您想使用 JVM 编程语言并且想学习不同的方法,您可以尝试 scala 的 xml 转换库。

一些博客文章:

http://scala.sygneca.com/code/xml-pattern-匹配

http://debasishg .blogspot.com/2006/08/xml-integration-in-java-and-scala.html

Although probably XSLT is the best way to do this, if you want to use a JVM programming language and you want to learn a different approach, you can try scala's xml transformation library.

Some blog posts:

http://scala.sygneca.com/code/xml-pattern-matching

http://debasishg.blogspot.com/2006/08/xml-integration-in-java-and-scala.html

溺孤伤于心 2024-08-30 13:40:07

XSLT 最终成为了前进的方向。它实际上非常容易使用,w3schools 示例是一个很好的起点。

XSLT was the way forward in the end. Its actually pretty easy to use and the w3schools example is a good place to start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文