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
我尝试过使用DocumentBuilderFactory,但是当我可以访问name标签的值时,我无法访问延迟/批处理部分中的值。这是我用过的代码
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.
调用getElementByName(“type”)或“parameters”似乎没有返回任何我可以使用的东西。我错过了什么,或者是否有更简洁的方法来处理现有的xml。
The need to be in the defined format to allow for marshalling and unmarshalling by Castor.
需要采用定义的格式以允许Castor进行编组和解组。
Any help would be much appreciated.
任何帮助将非常感激。
4 个解决方案
#1
5
There are a variety of ways to convert the XML.
有多种方法可以转换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.
1)您可以使用XSLT(XSL转换)来转换XML。它是一种基于XML的语言,用于转换其他XML文档,文本或HTML中的XML文档。语法很难学。但它是XML转换的强大工具。这是一个教程。对于使用XSLT和Java,我会推荐Saxon,它还附带了一个很好的文档。使用XSLT的最大优点是转换可以在单独的模板中外部化。因此,您的Java代码不会被翻译内容混淆。然而,如上所述,学习曲线明显更陡峭。
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
2)您可以使用XPath轻松选择节点。 XPath是一种用于选择XML文档中的节点的查询语言。顺便提一句,XPath也用在XSLT中。例如。 XPath查询
//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).
选择包含在节点参数中的所有参数值,该节点参数是包含具有值“确定性”的节点类型的延迟子节点。这是一个很好的Web应用程序,用于测试XPath查询。这是一个如何在Java中使用XPath的教程,以及一般的XPath。您可以使用XPath表达式在Java代码中选择正确的节点。恕我直言,这比直接使用DOM对象模型更具可读性和可维护性(正如您已经学习的那样,这也是不时的尴尬)。
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.
3)您可以使用Smooks进行XML转换。如果转换变得相当复杂,这尤其有用。 Smooks从输入XML填充对象模型,并使用Freemarker或XSL模板通过模板机制输出结果XML。 Smooks具有非常高的吞吐量,可用于高性能环境,如ESB(例如JBoss ESB,Apache ServiceMix)。可能会因为你的情况而被制服。
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.
4)您可以使用Freemarker进行转换。我没有这方面的经验,但是我听说它可以用得相当简单。请参阅文档的“声明性XML处理”部分(另请参阅“公开XML文档”以了解如何阅读源XML)。对我来说似乎相当简单。如果你试试这种方法,我很乐意听到它。
#2
1
This looks like a job for XPATH or some other XML transformation API.
这看起来像是XPATH或其他一些XML转换API的工作。
Check out: http://www.ibm.com/developerworks/library/x-javaxpathapi.html
请查看:http://www.ibm.com/developerworks/library/x-javaxpathapi.html
#3
0
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.
虽然可能XSLT是最好的方法,但是如果你想使用JVM编程语言并且想学习不同的方法,你可以试试scala的xml转换库。
Some blog posts:
一些博文:
http://scala.sygneca.com/code/xml-pattern-matching
http://debasishg.blogspot.com/2006/08/xml-integration-in-java-and-scala.html
#4
0
XSLT was the way forward in the end. Its actually pretty easy to use and the w3schools example is a good place to start.
XSLT最终是前进的方向。它实际上非常容易使用,w3schools的例子是一个很好的起点。
#1
5
There are a variety of ways to convert the XML.
有多种方法可以转换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.
1)您可以使用XSLT(XSL转换)来转换XML。它是一种基于XML的语言,用于转换其他XML文档,文本或HTML中的XML文档。语法很难学。但它是XML转换的强大工具。这是一个教程。对于使用XSLT和Java,我会推荐Saxon,它还附带了一个很好的文档。使用XSLT的最大优点是转换可以在单独的模板中外部化。因此,您的Java代码不会被翻译内容混淆。然而,如上所述,学习曲线明显更陡峭。
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
2)您可以使用XPath轻松选择节点。 XPath是一种用于选择XML文档中的节点的查询语言。顺便提一句,XPath也用在XSLT中。例如。 XPath查询
//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).
选择包含在节点参数中的所有参数值,该节点参数是包含具有值“确定性”的节点类型的延迟子节点。这是一个很好的Web应用程序,用于测试XPath查询。这是一个如何在Java中使用XPath的教程,以及一般的XPath。您可以使用XPath表达式在Java代码中选择正确的节点。恕我直言,这比直接使用DOM对象模型更具可读性和可维护性(正如您已经学习的那样,这也是不时的尴尬)。
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.
3)您可以使用Smooks进行XML转换。如果转换变得相当复杂,这尤其有用。 Smooks从输入XML填充对象模型,并使用Freemarker或XSL模板通过模板机制输出结果XML。 Smooks具有非常高的吞吐量,可用于高性能环境,如ESB(例如JBoss ESB,Apache ServiceMix)。可能会因为你的情况而被制服。
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.
4)您可以使用Freemarker进行转换。我没有这方面的经验,但是我听说它可以用得相当简单。请参阅文档的“声明性XML处理”部分(另请参阅“公开XML文档”以了解如何阅读源XML)。对我来说似乎相当简单。如果你试试这种方法,我很乐意听到它。
#2
1
This looks like a job for XPATH or some other XML transformation API.
这看起来像是XPATH或其他一些XML转换API的工作。
Check out: http://www.ibm.com/developerworks/library/x-javaxpathapi.html
请查看:http://www.ibm.com/developerworks/library/x-javaxpathapi.html
#3
0
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.
虽然可能XSLT是最好的方法,但是如果你想使用JVM编程语言并且想学习不同的方法,你可以试试scala的xml转换库。
Some blog posts:
一些博文:
http://scala.sygneca.com/code/xml-pattern-matching
http://debasishg.blogspot.com/2006/08/xml-integration-in-java-and-scala.html
#4
0
XSLT was the way forward in the end. Its actually pretty easy to use and the w3schools example is a good place to start.
XSLT最终是前进的方向。它实际上非常容易使用,w3schools的例子是一个很好的起点。