Here, you add a series of classes that should now be forming a standard pattern: an entity Transformer , the factory to create it TransformerFactory , and the exceptions that can be generated by each.
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. How can I do that? I tried this, but it doesn't work if there are additional newlines in the document:.
I have directed the ouput to System. You can see, that the output is not pretty. And the document usually doesn't come from a string, but from a file and gets modified heavily before I want to prettify it. This Transformer seems to be the right way, but I am missing something. Can you tell me, what I am doing wrong? It needs org. XMLSerializer and org. OutputFormat ;. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Prettify XML in org. Document to file Ask Question. Asked 9 years, 3 months ago. Active 9 years, 3 months ago. Viewed 7k times. I tried this, but it doesn't work if there are additional newlines in the document: import java. ByteArrayInputStream; import javax.
DocumentBuilderFactory; import javax. This effectively allows moving a subtree from one document to another unlike importNode which create a copy of the source node instead of moving it. When it fails, applications should use Document. Note that if the adopted node is already part of this document i. The descendants of the source Attr are recursively adopted.
Default attributes are discarded, though if the document being adopted into defines default attributes for this element name, those are assigned. The descendants of the source element are recursively adopted. No specifics. Note: Since it does not create new nodes unlike the Document. Parameters: source - The node to move into this document. Returns: The adopted node, or null if this operation fails, such as when the source node comes from a different implementation. Since: DOM Level 3 normalizeDocument void normalizeDocument This method acts as if the document was going through a save and load cycle, putting the document in a "normal" form.
As a consequence, this method updates the replacement tree of EntityReference nodes and normalizes Text nodes, as defined in the method Node. Otherwise, the actual result depends on the features being set on the Document. Noticeably this method could also make the document namespace well-formed according to the algorithm described in , check the character normalization, remove the CDATASection nodes, etc.
See DOMConfiguration for details. TRUE ; myDocument. If errors occur during the invocation of this method, such as an attempt to update a read-only node or a Node.
Note this method might also report fatal errors DOMError. When possible this simply changes the name of the given node, otherwise this creates a new node with the specified name and replaces the existing node with the new node as described below. If simply changing the name of the given node is not possible, the following operations are performed: a new node is created, any registered event listener is registered on the new node, any user data attached to the old node is removed from that node, the old node is removed from its parent if it has one, the children are moved to the new node, if the renamed node is an Element its attributes are moved to the new node, the new node is inserted at the position the old node used to have in its parent's child nodes list if it has one, the user data that was attached to the old node is attached to the new node.
When the node being renamed is an Element only the specified attributes are moved, default attributes originated from the DTD are updated according to the new element name. In addition, the implementation may update default attributes from other schemas. When the node being renamed is an Attr that is attached to an Element , the node is first removed from the Element attributes map.
Then, once renamed, either by modifying the existing node or creating a new one as described above, it is put back. Parameters: n - The node to rename. Returns: The renamed node. This is either the specified node or the new node that was created to replace the specified node.
That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy. Conceptually, it is the root of the document tree, and provides the primary access to the document's data. Creates a ProcessingInstruction node given the specified name and data strings.
This is a convenience attribute that allows direct access to the child node that is the document element of the document. The location of the document or null if undefined or if the Document was created using DOMImplementation. The configuration used when Document.
Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document. An attribute specifying the encoding used for this document at the time of the parsing.
An attribute specifying, as part of the XML declaration , the encoding of this document. An attribute specifying, as part of the XML declaration , whether this document is standalone. An attribute specifying, as part of the XML declaration , the version number of this document. Imports a node from another document to this document, without altering or removing the source node from the original document; this method creates a new copy of the source node.
This method acts as if the document was going through a save and load cycle, putting the document in a "normal" form. The DOMImplementation object that handles this document.
Creates an element of the type specified.
0コメント