简体   繁体   English

Java-Apache Commons Digester-编写xml

[英]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,我建议您:

  • Keep using Digester for the reading, and use a low-level XML writer class (such as XMLStreamWriter ) to write the data back. 继续使用Digester进行读取,并使用低级XML writer类(例如XMLStreamWriter )将数据写回。 This is suitable if your data is not that complex, and/or the output XML is a different structure to the input XML. 如果您的数据不是那么复杂,并且/或者输出XML与输入XML是不同的结构,则这是合适的。
  • Replace Digester with a full Java to XML mapping library (JAXB, JiBX etc.), which will both read and write the XML for you. 用完整的Java到XML映射库(JAXB,JiBX等)替换Digester,它将为您读取和写入XML。 This is suitable if your data is more complex, and/or the output XML is the same structure as the input XML. 如果您的数据更复杂,和/或输出XML与输入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.

相关问题 #java.lang.NoClassDefFoundError:org / apache / commons / digester / Digester - #java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester 在具有属性的XML中使用org.apache.commons.digester.Digester - Using org.apache.commons.digester.Digester in XML with attributes Eclipse - Jasper报告没有编译(java.lang.NoClassDefFoundError:org / apache / commons / digester / Digester) - Eclipse - Jasper report not compiling (java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester) 结合使用Apache Commons Digester和JSON - Using Apache Commons Digester with JSON 如何使用Apache Commons记录Digester? - How to log Digester with Apache Commons? IN java,commons-Digester 如何处理输入的 XML 文件? - IN java, how a commons-Digester process an input XML file? 使用apache -common-validator --java.lang.NoClassDefFoundError时出错:org / apache / commons / digester / Rule - Error using apache -common-validator --java.lang.NoClassDefFoundError: org/apache/commons/digester/Rule Commons Digester:如何使用Apache Lucene构建基于XML的复杂查询? - Commons Digester: How to build complex, XML-based queries with Apache Lucene? 有没有一种方法可以使用Apache Commons Digester从xml中添加Arraylist中键的所有值? - Is there a way to add all the values of a key in the Arraylist from xml using Apache Commons Digester? 我收到java.lang.NoClassDefFoundError:org / apache / commons / digester / Rule错误 - I am getting java.lang.NoClassDefFoundError: org/apache/commons/digester/Rule error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM