Search code examples
dom4j

How to change attribute values using DOM4J


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 ?


Solution

  • 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.