Nish
Nish

Reputation: 137

Remove non selected node from dom

Background: Converting html form output to pdf using iText and want to have only selected value. If there are three options and user has selected option C as below, would like to show C only in pdf.

<input type="radio" name="test" value="A">A
<input type="radio" name="test" value="B">B
<input type="radio" name="test" value="C" checked="true">C

For above set of string, as only option "C" is selected, I would like to get following output:

<input type="radio" name="test" value="C">C

Basically, want to remove non selected node. Tried following way and did not get expected output.

NodeList list = node.getChildNodes();
     for (int i = 0; i < list.getLength(); i++) {
          Element currentNode = null;
           if(list.item(i).getNodeType() == Node.ELEMENT_NODE && list.item(i).getNodeName().equals("input") )
               currentNode =(Element) list.item(i);
                if(conditionDefinedForNotchecked && currentNode != null)
                    currentNode.getParentNode().removeChild(currentNode);
       }

Upvotes: 0

Views: 40

Answers (1)

Emily Crutcher
Emily Crutcher

Reputation: 658

I love iText, but I still found creating an intermediate HTML format made my life much easier, simply because HTML debugging/viewing is much more advanced than PDF.

Suggested process:

  1. Real HTML
  2. JSoup to convert to simplified HTML stripped of all unused elements + all sizes specified
  3. IText output.

Upvotes: 1

Related Questions