[英]Java - Apache commons Digester - write xml
I'm using Apache Commons Digester (with annotations) in order to load an XML file into a Java class. 我正在使用Apache Commons Digester(带有批注)来将XML文件加载到Java类中。 Everything works correctly. 一切正常。 Now, I need to update the XML file. 现在,我需要更新XML文件。 I have to change (in Java) the value of a property, and then to write out the new XML file. 我必须更改(在Java中)属性的值,然后写出新的XML文件。 How could I do? 我该怎么办? As far as I can see, Digester API is not designed for this purpose. 据我所知,Digester API并非为此目的而设计的。
Edit: reading the answers, I understand that I did not give enough informations. 编辑:阅读答案,我了解我没有提供足够的信息。 My XML file is a configuration file for a program A, so I really need its content when I launch program A. Then, I have another GUI program B that is able to modify this configuration file, it just takes some input from the user and modifies the relative fields on the XML file. 我的XML文件是程序A的配置文件,因此启动程序A时确实需要它的内容。然后,我有另一个GUI程序B能够修改此配置文件,它只需要用户输入一些信息,然后修改XML文件上的相对字段。
XML modification (usually called XML transformation) is best handled in XSLT standard . XML修改(通常称为XML转换)最好在XSLT标准中处理。 Apache Xalan is (one of) the Java libraries that implement this standard Apache Xalan是实现此标准的Java库之一)
As you have found, Digester is a read-only tool - it provides a mapping from XML to Java classes, but not the reverse. 如您所见,Digester是一个只读工具-它提供了从XML到Java类的映射,但不是反向映射。 If you need to read the XML into Java classes and then write it back to XML again, I would suggest either: 如果您需要将XML读取到Java类中,然后再次将其写回到XML,我建议您:
Don't know enough about what you are doing to really recommend one of those approaches over the other. 对您在做什么的了解不足,以至于不能真正推荐其中一种方法。
If you don't actually need the data in Java classes at all and are just trying to transform it, then XSLT as @sharonbn says is also a good solution. 如果您实际上根本不需要Java类中的数据,而只是尝试对其进行转换,那么@sharonbn所说的XSLT也是一个很好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.