1

I have an xml which has a simple set of data. This data is displayed in a simple table and each row of data is assigned an ID in the table based on the position in the xml ( <xsl:value-of select="position()" /> ). I cant add an id attribute to the data because its not my data, but I need to locate elements based on this position and remove them.

public class Delete extends HttpServlet {

private final String XML_FILE = "data.xml";

public void init() throws ServletException {

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Disable browser caching
    response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);

    String index = request.getParameter("delete");

    try {
        // Load the current data.xml
        SAXBuilder builder = new SAXBuilder();
        Document xml_document = builder.build(new File(getServletContext().getRealPath("/") + XML_FILE));

        Element root = xml_document.getRootElement();
        root.removeChild(index);


        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml_document, new FileWriter(getServletContext().getRealPath("/") + XML_FILE));
    }
    catch(Exception ex) {}

    // Once we have processed the input we were given
    // redirect the web browser to the main page.
    response.sendRedirect("/");
}

public void destroy() {

}

}

This code does not remove the correct data. Anyone know how to find the child of the root element by its position?

@rolfl

int index = Integer.parseInt(delete);
Element root = xml_document.getRootElement();
root.getChildren().remove(index);

This does not remove any elements.

Finchy70
  • 441
  • 9
  • 25

2 Answers2

1

Your problem here is that the process is getting the index to remove as a string, and that's then calling the removeChild(String) method .... which looks for the first child that has an element tag name of whatever (string) value is in the index.

What you want to do, instead, is to convert the index to an int, and then treat the children of the root as a List.... something like:

int index = Integer.parseInt(request.getParameter("delete"));
root.getChildren().remove(index);

See the documentation for getChildren().

rolfl
  • 17,539
  • 7
  • 42
  • 76
0

This is how I got it to work. Not sure if its a great solution but it works.

        SAXBuilder builder = new SAXBuilder();
        Document xml_document = builder.build(new File(getServletContext().getRealPath("/") + XML_FILE));

        // Get root element
        Element root = xml_document.getRootElement();

        // Create a list of the children of the root element
        List<Element> kids = root.getChildren();

        // Interate through list of elements and delete (detach) the element at position index.
        int i = 1;
        for (Element element : kids)
        {
            if(i == index)
            {
                element.detach();
                break;
            }
            else
            {
            i = i + 1;
            }               
        }

I got the root element with

Element root = xml_document.getRootElement();

Made a list of it's children elements with

List<Element> kids = root.getChildren();

Then iterated through this list until I reached the index of the element to delete then did .detach on this element

            int i = 1;
            for (Element element : kids)
            {
                if(i == index)
                {
                    element.detach();
                    break;
                }
                else
                {
                i = i + 1;
                }               
            }

If anyone can update this to show an easier way to remove the element please do so. It feels like there must be an easier way to detach an element without the iteration. Anyway, as I said it works.

Finchy70
  • 441
  • 9
  • 25