The XML file that I am having trouble with:
<root>
<datbitmap>
<paract>
<action actiontype="startext" eventtype="mouseldouble" program="GISStarter" parameter="LAT=11&Lon=11"/>
</paract>
<parbmp bitmap="bmp0044.bmp"/>
<parbox type="none" halign="center" valign="center"/>
<parcol fore="-3" back="1" fill="-3"/>
<pardec min="0" max="31" accrule="0"/>
<parlay layer="0"/>
<paropt selectable="1" showstatesyms="1" showstatecols="1" passformula="0" keepformula="0" showoplayer="0"/>
<parrct hpos="0" vpos="0" width="56" height="62"/>
<partbl>
</partbl>
<partrf hpos="44" vpos="1134" hscale="1" vscale="1" angle="0"/>
</datbitmap>
<dattext>
<paract>
<action actiontype="default" eventtype="mouseldouble" dpadr=""/>
</paract>
<parbox type="none" halign="left" valign="top"/>
<parcol fore="0" back="-3" fill="-3"/>
<pardec min="0" max="31" accrule="0"/>
<parfnt family="helvetica" bold="0" italic="0" underline="0" size="type:3;p:16"/>
<parlay layer="0"/>
<paropt selectable="1" showstatesyms="1" showstatecols="1" showstateprefix="1" showstatesuffix="1" passformula="0" keepformula="0" formularesulttxt="0" windowtitletxt="0" showoplayer="0"/>
<parrct hpos="0" vpos="0" width="0" height="0"/>
<partbl>
</partbl>
<partrf hpos="109" vpos="1155" hscale="1" vscale="1" angle="0"/>
<partxt text="GIS"/>
</dattext>
我正在尝试修改第一个操作标记中的属性参数。 代码:
Document xmlData = getDocument("data.xml");
NodeList actionList = xmlDoc.getElementsByTagName("action");
String attribute = new String("LAT=" + latitudine + "&Lon=" + longitudine);
for(int j = 0; j < actionList.getLength(); j++) {
Element actionElement = (Element) actionList.item(j);
if(actionElement.hasAttribute("parameter")) {
actionElement.setAttribute("parameter", attribute);
break;
}
}
正在发生的事情: 1)列表只有一个元素(dattext中的action标记) 2)if(actionElement.hasAttribute(“parameter”))返回true。
运行代码后,xml文件保持不变。
如何修改我想要的属性?
答案 0 :(得分:0)
如果你想获得String并保存,或者其他:
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(document),
new StreamResult(buffer));
String out= buffer.toString();
如果您想直接发送文件
Transformer trans = TransformerFactory.newInstance().newTransformer();
Result out = new StreamResult(new File("result.xml"));
Source in = new DOMSource(doc);
trans.transform(in, out);
如你所见,有一些选择......