Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd like to propose you a recursive solution, which make use of <code>Node#replaceChild</code> method to replace a node with a new tag:</p> <pre><code>public static void paintAllNodes(Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { Element somethingElement = node.getOwnerDocument().createElement("something"); somethingElement.setAttribute("style", "background-color:red"); node.getParentNode().replaceChild(somethingElement, node); somethingElement.appendChild(node); NodeList nodeList = node.getChildNodes(); for (int i = 0; i &lt; nodeList.getLength(); i++) { paintAllNodes(nodeList.item(i)); } } } </code></pre> <p>This is my main:</p> <pre><code>public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("document.xml")); paintAllNodes(document.getDocumentElement()); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result); } </code></pre> <p>I've tested it with this xml:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;title&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;title&lt;/h1&gt; &lt;div&gt;test&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>My main has printed out this new xml, which seems to be what you want:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;something style="background-color:red"&gt;&lt;html&gt; &lt;something style="background-color:red"&gt;&lt;head&gt; &lt;something style="background-color:red"&gt;&lt;title&gt;title&lt;/title&gt;&lt;/something&gt; &lt;/head&gt;&lt;/something&gt; &lt;something style="background-color:red"&gt;&lt;body&gt; &lt;something style="background-color:red"&gt;&lt;h1&gt;title&lt;/h1&gt;&lt;/something&gt; &lt;something style="background-color:red"&gt;&lt;div&gt;test&lt;/div&gt;&lt;/something&gt; &lt;/body&gt;&lt;/something&gt; &lt;/html&gt;&lt;/something&gt; </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload