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.
//param[@name="id"],//param[@name="type"],//param[@name="bus"], etc.