1

i'm trying to modify some parameters of a xml file from a java file. In all the posts i found people modify these parameters searching for it by tag name, but in my case it doesn't work.

An example of my xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<comp>
    <params>
        <param name="id">1</param>
        <param name="type">aaaa</param>
        <param name="bus">123</param>
    </params>
</comp>

How i can access the parameters by "name"?

Finally my code in java is:

public void modifyXml(){
    String path = "C:\\Workspace\\Project\\data.xml";
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(path);
    Node comp = doc.getFirstChild();
    Node params = doc.getElementsByTagName("params").item(0);
    loadParams(params);
}

private void loadParams(Node params) {
    NodeList list = params.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node url =   list.item(i);
        String nodeName = url.getNodeName();
        NamedNodeMap attributes = url.getAttributes();

        //System.out.println(attributes.item(i));
        if (nodeName.equalsIgnoreCase("param") && url.getFirstChild()!=null) {
            ServerProperties.getExternalParamsMap().put(attributes.getNamedItem("name").getNodeValue(), url.getFirstChild().getNodeValue());
            System.out.println((String) url.getFirstChild().getNodeName());
            System.out.println(url.getFirstChild().getNodeValue());
         }
    }
}

It prints me the node's values but not the parameter name. Parameters name appear like: "#text"

Thanks!

Regards!!

Eric P.

1
  • 1
    Don't write it by hand with DOM. Use XPath with these expressions: //param[@name="id"], //param[@name="type"], //param[@name="bus"], etc. Commented Aug 18, 2016 at 6:39

2 Answers 2

1

You should use XPath to simplify coding... below is the code based on XPath and vtd-xml.

import com.ximpleware.*;

public class accessName {

    public static void main(String[] args) throws VTDException{
        VTDGen vg = new VTDGen();
        if (!vg.parseFile("input.xml", false))
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/param[@name='id']/text()");
        int i=0;
        while((i=ap.evalXPath())!=-1){
            System.out.println("text value "+vn.toString(i));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Now it works for me:

    String path = "C:\\Workspace\\Project\\data.xml";
    File file = new File(path);
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
            .newInstance();
    javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(file);
    Node id = (Node) document.getElementsByTagName("param").item(0);
    id.setTextContent("xxxxxxx");
    System.out.println(usr.getTextContent());

This will modify the "id" parameter

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.