I want to remove the JOB node with JOBNAME "One" from the following file:

<SETTINGS>
<SYSTEM>
<VERSION>3.1<VERSION>
</SYSTEM>
<JOB>
<JOBNAME>One</JOBNAME>
</JOB>
<JOB>
<JOBNAME>Two</JOBNAME>
</JOB>
</SETTING>

The code that is supposed to remove it looks like this:

try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(str_fileName));

            System.out.println("starting parser");
            NodeList list = doc.getElementsByTagName("*");
            for (int i = 0; i <
                    list.getLength(); i++) {
                //Get Node
                Node node = (Node) list.item(i);
                // Look through entire settings file
                if (node.getNodeName().equalsIgnoreCase("JOB")) {
                    NodeList childList = node.getChildNodes();
                    // Look through all the children
                    for (int x = 0; x < childList.getLength(); x++) {
                        Node child = (Node) childList.item(x);
                        // Only the JOB children will be looked in.
                        System.out.println("looking for JOBNAME and " + jobName);
                        if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equalsIgnoreCase("JOBNAME") && child.getTextContent().equalsIgnoreCase(jobName)) {
                            System.out.println(" found JOBNAME and " + jobName);
                            // Delete job node here
                            //node.getParentNode().removeChild(node);
                            doc.getRootElement().getChild("row").removeChild("address");
                        }
                    }
                }
            }
            
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException saxe) {
            saxe.printStackTrace();
        }

When I run the code, it doesn't throw any exceptions, but it doesn't actually remove the node either.

Note that I want to remove the JOB node, as well as all its contents.

Can anyone help with this?

Recommended Answers

All 4 Replies

read xml file using following code...

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

 String remElement = bf.readLine();

then equals remElement = "WHICH STRING YOU WANT";

THEN USING BELOW LINE AND REMOVE CONTENT AND TAG

Element element = (Element)doc.getElementsByTagName(remElement).item(0);
//        Remove the node
        element.getParentNode().removeChild(element);
//              Normalize the DOM tree to combine all adjacent nodes
        doc.normalize();

read xml file using following code...

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

 String remElement = bf.readLine();

then equals remElement = "WHICH STRING YOU WANT";

THEN USING BELOW LINE AND REMOVE CONTENT AND TAG

Element element = (Element)doc.getElementsByTagName(remElement).item(0);
//        Remove the node
        element.getParentNode().removeChild(element);
//              Normalize the DOM tree to combine all adjacent nodes
        doc.normalize();

I get what you mean. I've seen this code online. I just don't know how I can use it to remove a JOB node from the file structure I specified, where there is more than one JOB node on the same level, with the only way to tell them apart is by one of their children called JOBNAME.

Sorry if I seem stupid, but I just don't see how the code you supplied is going to work, as setting remElement = "JOB" would just remove ALL the jobfiles, wouldn't it?

YES YOU CORRECT....

BUT one case you find job node and check whether it is contain string that you need to remove..

Element element = (Element)doc.getElementsByTagName(remElement).item(0);


here you specify exactly which occurrence of tag item(0);

the occurence you need to find out by counting(++) tag and match which tag(i) contains string that you remove...

all i said that what i know so far...

b/w i also didn't tried it before...

just for helping you!! :-)

Thanks for the advice. I got it working using the following:

public boolean deleteJob(String jobName) {

        String fileName = str_fileName;

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(fileName));

            System.out.println("starting parser");
            NodeList list = doc.getElementsByTagName("*");
            for (int i = 0; i <
                    list.getLength(); i++) {
                //Get Node
                Node node = (Node) list.item(i);
                // Look through entire settings file
                if (node.getNodeName().equalsIgnoreCase("JOB")) {
                    NodeList childList = node.getChildNodes();
                    // Look through all the children
                    for (int x = 0; x < childList.getLength(); x++) {
                        Node child = (Node) childList.item(x);
                        // Only the JOB children will be looked in.
                        System.out.println("looking for JOBNAME and " + jobName);
                        if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equalsIgnoreCase("JOBNAME") && child.getTextContent().equalsIgnoreCase(jobName)) {
                            System.out.println(" found JOBNAME and " + jobName);
                            // Delete job node here
                            node.getParentNode().removeChild(node);
                        }
                    }
                }
            }

            try {
                OutputFormat format = new OutputFormat(doc);
                FileWriter fw = new FileWriter(fileName);
                XMLSerializer serial = new XMLSerializer(fw, format);
                serial.asDOMSerializer();
                serial.serialize(doc.getDocumentElement());
                fw.flush();
                fw.close();
            } catch (IOException io) {
                io.printStackTrace();
                return false;
            }

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SAXException saxe) {
            saxe.printStackTrace();
        }

        return true;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.