Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3308642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:32:44+00:00 2026-05-17T21:32:44+00:00

I want to read DOM document using Stax stream readers and write it using

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:32:45+00:00Added an answer on May 17, 2026 at 9:32 pm

    It’s probably because you are returning when you find the “references” element. Maybe break is what you meant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read an xml file, apply a transform, then write to another
I want to read and write from serial using events/interrupts. Currently, I have it
I want to read, write and create Spreadsheets in the Open Document Format with
I want to read line n1->n2 from file foo.c into the current buffer. I
I want to read an specific xml node and its value for example <customers>
I have xml files I want to read in and store in the database.
I want to read the contents of a URL but don't want to hang
I want to read all items of a feed in C#. The solutions I've
I want to read UTF-8 input in Perl, no matter if it comes from
Yes I want to read a simple a logfile into a TStringList and that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.