Writing into xml file in java
The Attribute class has a couple of different constructors. The simplest one is Attribute String name, String value. Once you have all of your elements add to your root element you can turn it into a Document. Document will take a Element as an argument in it's constructor. You can use a Serializer to write your XML to a file. You will need to make a new output stream to parse in the constructor of Serializer.
FileOutputStream; import java. IOException; import java. InputStreamReader; import java. StringWriter; import javax. DocumentBuilder; import javax. DocumentBuilderFactory; import javax. ParserConfigurationException; import javax. OutputKeys; import javax. Transformer; import javax.
TransformerException; import javax. TransformerFactory; import javax. DOMSource; import javax. StreamResult; import org. Although I need a solution were the XML format is retained, example editing a cache xml with below format should give me an output with the same format.
Can you please suggest a way to do it? Here i have a Question.. How to implement servlet in Struts Frame work.. Could explain clearly.. Your email address will not be published. Prev AtomicInteger in Java. Pankaj I love Open Source technologies and writing about my experience about them is my passion.
Follow Author. Comments MrBigglesworth says:. June 27, at pm. For example, to get indented output, you can invoke the following method:. In the case below, TransformationApp01 is run on the file foo. As mentioned in Creating a Transformer , this transformer has not actually changed anything, but rather has just performed the identity transform, to generate a copy of the source. It is also possible to operate on a subtree of a DOM. In this section, you will experiment with that option.
The code discussed in this section is in TranformationApp The first step is to import the classes you need to get the node you want, as shown in the following highlighted code:. The next step is to find a good node for the experiment. In Creating a Transformer , the source object was constructed from the entire document by the following line of code.
However, the highlighted line of code below constructs a source object that consists of the subtree rooted at a particular node. In the case below, TranformationApp02 is run on the file foo. At this point, you have seen how to use a transformer to write out a DOM and how to use a subtree of a DOM as the source object in a transformation.
0コメント