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 9214063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:53:33+00:00 2026-06-18T01:53:33+00:00

I am using DOM4J to parse my XML file and change the attribute values.

  • 0

I am using DOM4J to parse my XML file and change the attribute values.

Here I am facing some difficulties while changing the attributes. After generating new updated XML file, it seems that DOM4J failed to change those attributes.

Here is a sample xml file that I want to parse.

<Form Width="69977" RowHeight="37998" ColWidth="69977" RowCount="7"
        RowOffset="0" ColCount="3" ColOffset="0" MarginLeft="0" MarginTop="0"
        MarginRight="0" MarginBottom="0" PrintOrder="0" Orientation="Portrait"
        Rotation="0" DataSource="">
        <FileVersion Major="2" Minor="1" />
        <DocumentName />
        <Comment />
        </Form>

Here In the form tag I want to change the value of MarginRight and MarginTop.

And Here is the code that I am using to change the attributes…

//ITERATE OVER GIVEN NODE NAMES AND MAKE SEARCH OF IT
            for(String nodeName : nodeNameSet){
                nodeList = document.getElementsByTagName(nodeName);


                if(nodeList!=null && nodeList.getLength()>0){
                    node = nodeList.item(0);

                    //ITERATE OVER ATTRIBUTE NAME-VALUE MAP AND REPLACE THE EXISTING VALUEs
                    attribNameValMap = nodeArrribMap.get(nodeName);
                    attribNameSet = attribNameValMap.keySet();

                    if (node!=null && node.getNodeType() == Node.ELEMENT_NODE) {
                        for(String attribute : attribNameSet){
                            element = (Element)node;
                            element.setAttribute(attribute,attribNameValMap.get(attribute));
                        }                       
                    }else{
                        throw new SELSystemException("Error while parsing XML File : Required Node Type : ELEMENT : Found Node Type :"+node.getNodeType());
                    }

                }

            }

            //CREATE A TRANSFORMER TO TRANSFORM THE XML TO INPUTSTREAM
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

            //PREAPRE A SOURCE TO WRITE THE  FILE BACK
            DOMSource domSource = new DOMSource(document);
            //WRITE THE UPDATED CONTENT TO A STRINGWRITER
            Writer writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            //TRANSFORM THE CONTENT TO STRINGBUILDER
            transformer.transform(domSource, result);
            //CONVERT STRINGBUILDER DATA TO STRING
            returnStream = writer.toString();

here in the returnStream variable I compared the file stream to old xml file. But it seems that the attributes are not changed.

Can anyone suggest me better idea to change the attribute with DOM4J ?

  • 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-06-18T01:53:35+00:00Added an answer on June 18, 2026 at 1:53 am

    The above mentioned code is working fine.

    Actually after checking the generated XML file once again, i found that there are two “” tag. So the code above had changed the attributes in first tag which I have not noticed.

    So the code mentioned above is working fine.

    Thanks.

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

Sidebar

Related Questions

I'm using dom4j to parse a xml file in order to get some data
I'm using dom4j to parse my xml. Let's say I have something like this:
I am using dom4j to parse xml. Then I want to be able to
I am trying to use dom4j to validate the xml at http://www.w3schools.com/Schema/schema_example.asp using the
Specifically, I was using dom4j to read in KML documents and parsing out some
I'm using dom4j to create XML documents from entities. I'm doing something like: Entity
I am using DOM4J to do analysis of two XML elements. The elements are
In a Java program, i am processing an xml using dom4j. Now, I want
I have some classes that already use DOM4J to read XML files and provide
I'm working on an XML message process program using dom4j, and encounter a problem

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.