tags:

views:

25

answers:

1

I want to read DOM document using Stax stream readers and write it using Stax stream writers. I want to modify xml file and change some element values I want the cursor to point at a certain element in xml file befor building dom tree I wrote this code but the xml file did not modified can anybody help me ?

FileInputStream input = new FileInputStream("cv.xml"); XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(input); BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    //-*-*- get new entries from input stream
    System.out.println("<< CV >>\n -> Modify the first reference\n    ** Modify The Name **");
    System.out.print("    Enter degree : ");
    String degree = in.readLine();
    System.out.print("    Enter first name : ");
    String fName = in.readLine();
    System.out.print("    Enter last name : ");
    String lName = in.readLine();
    System.out.println("    ** Modify The Address ** ");
    System.out.print("    Enter new city : ");
    String newCity = in.readLine();
    System.out.print("    Enter new country : ");
    String newCountry = in.readLine();

    //-*-*- let the reader point at the first "reference" element
    int eventType;
    boolean ref = false, fname = false;
    while (reader.hasNext()) {
        eventType = reader.next();
        switch (eventType) {
            case XMLEvent.START_ELEMENT:
                if (reader.getLocalName().equalsIgnoreCase("references"))
                    return;
        }
    }
    //-*-*- build DOM trees using Stax stream reader
    Document doc = new DOMConverter().buildDocument(reader);
    reader.close();
    input.close();
    //-*-*- start modification
    Element firstRef = (Element)doc.getElementsByTagName("reference").item(0);
    NodeList name = (NodeList)firstRef.getElementsByTagName("name");
    //-*-*- modify the degree (Dr. , Eng. , Dev. ,etc)
    Attr att = (Attr)name.item(0).getAttributes().item(0);
    ((Node)att).setNodeValue(degree);
    //-*-*- modify first name
    NodeList firstName = (NodeList)firstRef.getElementsByTagName("fname");
    NodeList firstNameChilds = (NodeList)firstName.item(0).getChildNodes();
    ((Node)firstNameChilds.item(0)).setNodeValue(fName);
    //-*-*- modify last name
    NodeList lastName = (NodeList)firstRef.getElementsByTagName("lname");
    NodeList lastNameChilds = (NodeList)lastName.item(0).getChildNodes();
    ((Node)lastNameChilds.item(0)).setNodeValue(lName);
    //-*-*- modify city
    NodeList city = (NodeList)firstRef.getElementsByTagName("city");
    NodeList cityChilds = (NodeList)city.item(0).getChildNodes();
    ((Node)cityChilds.item(0)).setNodeValue(newCity);
    //-*-*- modify country
    NodeList country = (NodeList)firstRef.getElementsByTagName("country");
    NodeList countryChilds = (NodeList)country.item(0).getChildNodes();
    ((Node)countryChilds.item(0)).setNodeValue(newCountry);

    //-*-*- write DOM document
    FileOutputStream out = new FileOutputStream("cv.xml");
    XMLStreamWriter sw = XMLOutputFactory.newInstance().createXMLStreamWriter(out);

    new DOMConverter().writeDocument(doc, sw);
    sw.close();
    out.close();
A: 

It's probably because you are returning when you find the "references" element. Maybe break is what you meant.

Cameron Skinner
no, I meant return, I want to stop while loop when the XMLStreamReader match "references" element. so, the dom tree begin from this element
Dev. developer
Return will exit out of the entire method. I'm pretty sure you want `break` to get out of that while loop. Try putting some debugging statements after that loop and you'll see what I mean.
Cameron Skinner
yes you are right about return but when I use break it will get out of switch but still in the loop after the cursor match references element and will reach the end of the document
Dev. developer
Right, so you need a better loop control variable. Try a boolean that is false at the beginning and set to true when you find `references`. You will still need to check for `reader.hasNext()`.
Cameron Skinner
I tried this :int eventType; boolean ref = false, fname = false; while (reader.hasNext()) { eventType = reader.next(); switch (eventType) { case XMLEvent.START_ELEMENT: if (reader.getLocalName().equalsIgnoreCase("references")) { ref = true; break; } } if(ref) break; } System.out.println("I am here");
Dev. developer
but it threw two exception : java.lang.NoClassDefFoundError: org/codehaus/stax2/ri/Stax2ReaderAdapter
Dev. developer
Cameron Skinner
and java.lang.ClassNotFoundException: org.codehaus.stax2.ri.Stax2ReaderAdapter it's the first time I use Dom converter, I don't know where I write a peroper code or not
Dev. developer
OK, the exceptions are probably a bit beyond the scope of this question. Try searching for similar questions and see if someone else has had the same problem.
Cameron Skinner
OK, thank you ...
Dev. developer