0

For example I have this file XML :

<http auto-config="true">
        <intercept-url access="ROLE_ADMIN,ROLE_USER" pattern="/"/>
        <intercept-url access="ROLE_ADMIN,ROLE_USER" pattern="/index*"/>
        <intercept-url access="ROLE_ADMIN" pattern="/page1"/>
        <intercept-url access="ROLE_ADMIN" pattern="/page2"/>
        <intercept-url access="ROLE_ADMIN" pattern="/page33"/>

</http>

I want to change the text of the Element "access" of this line :

<intercept-url access="ROLE_ADMIN" pattern="/page33"/>

so I create this code :

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.xml.internal.ws.org.objectweb.asm.Attribute;

public class ChangeXMLFile {

    public static void main(String argv[]) {

       try {
        String filepath = "D:\\Myfile.xml";
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);


        Node company = doc.getFirstChild();

        Node staff = doc.getElementsByTagName("intercept-url").item(0);

        NodeList list = staff.getChildNodes();

        for (int i = 0; i < list.getLength(); i++) {

                   Node node = list.item(i);


           if ("pattern".equals(node.getNodeName())) {
                if(("/page33").equals(node.getTextContent())){


                    NamedNodeMap attr = staff.getAttributes();
                    Node nodeAttr = attr.getNamedItem("access");
                    nodeAttr.setTextContent("ROLE_ANONYM");
                }

           }




        }


        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);

        System.out.println("Done");

       } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
       } catch (TransformerException tfe) {
        tfe.printStackTrace();
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } catch (SAXException sae) {
        sae.printStackTrace();
       }
    }
}

But I have no change. How to let the program read each line of the xml file and change the "access" attribute according to the content Text of "pattern" ?

2 Answers 2

1

You've got the hierarchy levels all wrong. Try something like this instead:

        DocumentBuilderFactory docFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(...;

        NodeList list = doc.getElementsByTagName("intercept-url");

        for (int i = 0; i < list.getLength(); i++) {
            Element node = (Element) list.item(i);
            Attr pattern = node.getAttributeNode("pattern");
            if (pattern != null && pattern.getValue().equals("/page33")) {
                node.setAttribute("access", "ROLE_ANONYM");
            }
        }

        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(...);
        transformer.transform(source, result);
Sign up to request clarification or add additional context in comments.

3 Comments

heyyy I was first with the same good answer (even earlier), why is it yours that have been validated? I file a claim :)
That's life, cruel and unfair :-) But you missed the part that attribute nodes are not anyones children, so it wouldn't have worked with the existing attribute logic.
Ok that's true I did not even try to understand why and if the attributes could possiby be used through a NamedNodeMap. The main problem was so obvious I stopped here.
0

You should check in debug,

    Node staff = doc.getElementsByTagName("intercept-url").item(0);

    NodeList list = staff.getChildNodes();

list is empty: staff is your first "intercept-url" node, so when you try to get its children the resulting list is empty.

Try instead something like this:

    NodeList staffList = doc.getElementsByTagName("intercept-url");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.