这是我的第一个问题,请原谅我任何错误。
我想通过我在这样构建的XML文件中的代码修改特定值:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!-- STATION settings -->
<!-- Example: <add key="settingName" value="settingValue"/> -->
<add key="station_id" value="0001"/>
<add key="debugging" value="FALSE"/>
<add key="lang" value="1"/>
<add key="lokales_lesen_schreiben" value="FALSE"/>
<add key="data_path" value="/pdac/"/>
<add key="data_dbconn" value="\my documents\pdaControl\pdac_local.sdf"/>
<add key="layout" value="actlayout_1"/>
<add key="connection_timeout" value="10"/>
<add key="soundfile_good" value="[APP]\Media\iwrgood.wav"/>
<add key="soundfile_bad" value="[APP]\Media\iwrbad.wav"/>
<add key="soundfile_quest" value="[APP]\Media\iwrask.wav"/>
<!--<add key="device_assembly" value="[APP]\iw_Symbol.dll"/>
<add key="device_class" value="iw_Symbol.clsSymbol"/>-->
<add key="device_execute"
value="ENABLE_DEFAULTCODES;SET_2OF5_RANGE(0,0),ENABLE_REDUNDANCY(3OF9,2OF5);ENABLE_VERIFY(3OF9,2OF5);ENABLE_CHECKTRANS(3OF9,2OF5)"/>
<add key="device_action_good" value="LED_GREEN;VIBRATION_SHORT"/>
<add key="device_action_bad" value="BEEP_MID(1);LED_RED;VIBRATION_LONG"/>
<add key="device_action_quest" value="BEEP_MID(1);LED_RED;VIBRATION_MID"/>
</appSettings>
</configuration>
这里我要编辑“值”条目。
目前我尝试用此代码解决我的问题,但XML没有改变
try
{
String filepath = path;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = docFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new File(path));
Node configuration = document.getFirstChild();
Node add = document.getElementsByTagName("add").item(0);
//update
NodeList list = configuration.getChildNodes();
for (int i = 0; i < list.getLength(); i++){
Node attr = list.item(i);
if (vordererWert.contains(attr.getNodeName())){
attr.setTextContent(Eingabe);
}
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
} catch (ParserConfigurationException pce)
{
pce.printStackTrace();
} catch (IOException ioe)
{
ioe.printStackTrace();
} catch (SAXException sae)
{
sae.printStackTrace();
} catch (TransformerConfigurationException tce)
{
tce.printStackTrace();
} catch (TransformerException te)
{
te.printStackTrace();
}
}
我希望你们能帮助我:)。