0

I got some HTML in a String named content.

a simple HTML Code:

<table>
    <tr role="row">
       <td role="gridcell">Hello</td>
       <td role="gridcell">Another one</td>
    </tr>
    <tr role="row">
       <td role="gridcell">Second row</td>
       <td role="gridcell">Second column in second row</td>
    </tr>
</table>

Now I want to build a XML Document with Java.

I got some Methods to add tags, values and attributes:

private static Element appendNodeValAttr(Document doc, Node root, String tagName, String value, String... attrs)
    {
        Element e = doc.createElement(tagName);
        if (value != null) {
            e.appendChild(doc.createTextNode(value));
        }
        for(int i = 0; i < attrs.length; i+=2){
            e.setAttribute(attrs[i], attrs[i+1]);
        }
        root.appendChild(e);

        return e;
    }

for example:

appendNodeValAttr(doc, html, "font", "Some Text", "size", "30");

will return an Element with..

<font size="30">Some Text</font>

How can I change the String with all the tags, to Elements, which will be added finally to my document?

I really need some help.

Greetings.

3
  • Can you share the expected output ? Commented Dec 17, 2013 at 12:48
  • The expected output is the same like in the String. But I want to build the document with the appendNode Method. Commented Dec 17, 2013 at 12:50
  • You can parse the xml string using DocumentBuilder and get the document itself. Please refer stackoverflow.com/questions/17853541/… Commented Dec 17, 2013 at 12:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.