Java write dom to file




















With NodeFilter. The following example uses a custom DOM filter. The text content contains spaces and new line characters; therefore, we remove the unnecessary spaces with a regular expression. In the acceptNode method, we control which nodes to use by returning NodeFilter. The example reads the elements and the text of a continents. We will process elements and text nodes. Note that empty text such as indentation is considered text too. The processing is delegated to the traverseLevel method, which is called recursively.

We print the text data. Since we are interested only in the capital and population data, we skip all empty strings. After we have finished processing a branch, we go to the same level with setCurrentNode so that we can continue with another tree branch. Amit Jindal says:. June 12, at am. Vijay Kumar Kanta says:. July 2, at am. Priya says:.

September 13, at am. Hary says:. April 28, at am. July 30, at am. Praveen says:. March 11, at am. Maxwell Queiroz says:. November 14, at am. July 25, at am. Bala says:.

Because a transformation always has a source and a result, you then import the classes necessary to use a DOM as a source DOMSource and an output stream for the result StreamResult. Here, you create a transformer object, use the DOM to construct a source object, and use System.

You then tell the transformer to operate on the source object and output to the result object. In this case, the "transformer" is not actually changing anything. In XSLT terminology, you are using the identity transform, which means that the "transformation" generates a copy of the source, unchanged.

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.



0コメント

  • 1000 / 1000