954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Removing elements from a xml file

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

3.1OneTwo

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?

P00dle
Junior Poster
154 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

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();
musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 19
Solved Threads: 45
 

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?

P00dle
Junior Poster
154 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

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!! :-)

musthafa.aj
Posting Whiz
312 posts since Nov 2009
Reputation Points: 19
Solved Threads: 45
 

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;
    }
P00dle
Junior Poster
154 posts since Nov 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: