简体   繁体   中英

Why doesn't this Java code modify my XML file?

The XML file that I am having trouble with:


<root>
<datbitmap>
  <paract>
    <action actiontype="startext" eventtype="mouseldouble" program="GISStarter" parameter="LAT=11&amp;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>

I am trying to modify the attribute parameter in the first action tag. The code:

Document xmlData = getDocument("data.xml");
  NodeList actionList = xmlDoc.getElementsByTagName("action");
  String attribute = new String("LAT=" + latitudine + "&amp;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;
    } 
  }

Things that are happening: 1)The list has only one element(the action tag in dattext) 2)The if(actionElement.hasAttribute("parameter")) returns true.

After running the code the xml file is unchanged.

How to modify the attribute I want?

If you want to get the String and save, or whatever:

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();

If you want direct to a file

Transformer trans = TransformerFactory.newInstance().newTransformer();
Result out = new StreamResult(new File("result.xml"));
Source in = new DOMSource(doc);
trans.transform(in, out);

as you see, there are some options ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM