开发者

stax to DOM converter

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 fir开发者_运维知识库stRef = (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();


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜