Results 1 to 3 of 3
  1. #1
    scheilaad is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Modify and delete certain xml entries

    So i create an xml file wich is formated like this;

    Java Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Events>
    <Event>
    <eventid>1</eventid>
    <year>2010</year>
    <month>09</month>
    <day>19</day>
    <calendarid>2</calendarid>
    <eventname>birthday me</eventname>
    <starthour>21</starthour>
    <startminutes>00</startminutes>
    <stophour>03</stophour>
    <stopminutes>00</stopminutes>
    <description>birthdayparty</description>
    </Event>
    <Event>
    <eventid>2</eventid>
    <year>2010</year>
    <month>11</month>
    <day>23</day>
    <calendarid>2</calendarid>
    <eventname>birthday mom</eventname>
    <starthour>21</starthour>
    <startminutes>00</startminutes>
    <stophour>03</stophour>
    <stopminutes>00</stopminutes>
    <description>birthdayparty</description>
    </Event>
    </Events>
    now what i need to do is look for the correct event by looking at it's eventid (wich is unique), and then look for a node (like starthour, description or eventname) and modify this. but i've been at this for 3 days now and i can't seem to get it fixed.

    Java Code:
        
    public void saveEvents(File file ,Document moDoc, String filename) throws Exception {
    
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            src = new DOMSource(moDoc);
            String temp = filename + ".xml";
            res = new StreamResult(new File(temp));
            transformer.transform(src, res);
        }
    
            public void modEvents2(String filename, String mod, String newVal, String evId) throws Exception{
            String filepath = filename + ".xml";
            modDoc = getBuilder.parse(filepath);
            //Element root = modDoc.getDocumentElement();
    
            NodeList childNodes = modDoc.getElementsByTagName("Event");
    
            for (int i = 0; i < childNodes.getLength(); i++) {
                NodeList subChildNodes = childNodes.item(i).getChildNodes();
                for (int j = 0; j < subChildNodes.getLength(); j++) {
                    Node child = (Node) subChildNodes.item(j);
                    if (child.getNodeName().equalsIgnoreCase("eventid") && child.getTextContent().equalsIgnoreCase(evId)) {
                        if (subChildNodes.item(j).getTextContent().equals(mod)) {
                            subChildNodes.item(j).setTextContent(newVal);
                        }
                    }
                }
            }
            saveEvents(XmlFile, modDoc, filename);
        }
    any help would be greatly appreciated!

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    What exactly is the error? This loop looks very suspicious:

    Java Code:
    for (int j = 0; j < subChildNodes.getLength(); j++) {
                    Node child = (Node) [B]subChildNodes.item(j)[/B];
                    if (child.getNodeName().equalsIgnoreCase("eventid") && child.getTextContent().equalsIgnoreCase(evId)) {
                        if ([B]subChildNodes.item(j)[/B].getTextContent().equals(mod)) {
                            [B]subChildNodes.item(j)[/B].setTextContent(newVal);
                        }
                    }
                }
    because you're calling subChildNodes.item(j) three times, so what you have is

    Java Code:
    for (int j = 0; j < subChildNodes.getLength(); j++) {
                    Node child = (Node) [B]subChildNodes.item(j)[/B];
                    if (child.getNodeName().equalsIgnoreCase("eventid") && child.getTextContent().equalsIgnoreCase(evId)) && [B]child[/B].getTextContent().equals(mod)) {
                            [B]child[/B].setTextContent(newVal);
                        }
                    }
                }
    and obviously that if statement will never evaluate to true, because the child's text node can't be two different values at the same time.

    My guess is that you don't need Java at all, because you're opening the XML file, changing the DOM, and writing it back out to the same filename, right? If so, then just use XSL to change the value.

  3. #3
    scheilaad is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default thanks

    Dear NRitH,

    Thanks for replying, but i fixed it a way back, not sure how I did it back then. If I find my code, i will post it here for future use.

    Best regards,

    Scheilaad

Similar Threads

  1. JNDI - Getting the child entries of base DN
    By natu in forum Advanced Java
    Replies: 7
    Last Post: 08-06-2010, 12:01 PM
  2. Replies: 2
    Last Post: 04-20-2009, 09:00 AM
  3. Stoping repeated entries in JComboBox
    By MasterDeveloper in forum AWT / Swing
    Replies: 5
    Last Post: 04-05-2009, 01:07 PM
  4. Not able to delete MSSql database table entries
    By wickedrahul9 in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 01:14 PM
  5. How to Modify,Delete data in File Txt???
    By hungleon88 in forum Advanced Java
    Replies: 9
    Last Post: 09-24-2008, 04:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •