Trying to get my Java IRC Bot to parse and write to an preexisting XML file -


i'm total java virgin, , i've been stumbling surely in developing irc bot friends. far, i've gotten of features in working order. but, i'm wracking brain on problem here, bot far can reply link, every week, have change link in java file manually , recompile whole thing. so, want able parse pertinent values xml file in same directory bot's java files in, , able update same values through irc client.

import org.jibble.pircbot.*;  import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.xml.sax.*; import org.w3c.dom.*;    public class modbot extends pircbot {  static string inputfile = "./botdata.xml"; static string outputfile = "./botdata.xml";  public modbot() {     setlogin("modbot");     this.setname("modbot");     setversion(" "); }  public void onmessage(string channel, string sender, string login, string hostname, string message) {  if (message.equalsignorecase("!lolcat")) { sendmessage(channel, sender + "http://i.imgur.com/4ix4cul.jpg"); }  if (message.startswith("!updatelolcat ")) {     if(login.equals("mainmod"));     string changelolcat = message.substring(14);   } } } 

and xml

<?xml version="1.0" encoding="utf-8" standalone="no"?> <!doctype botdata [ <!element botdata (lolcat,partytime,start,end)> <!element lolcat (#pcdata)> <!element partytime (start,end)> <!element start (#pcdata)> <!element end (#pcdata)> ]> <botdata> <lolcat>http://i.imgur.com/4ix4cul.jpg</lolcat> <partytime> <start>8:45:30</start> <end>11:00:00</end> </partytime> </botdata> 

what want take whatever "changelolcat" is, , overwrite current link in xml, , way read same xml send what's in "lolcat" replying "!lolcat". i've been going through xpath , jdom , stuff, , can't make sense of it. i've read methods using xpath looks promising, , i'd prefer use because it's prettier read.

edit:

it worked, put in

try {documentbuilderfactory documentbuilderfactory = documentbuilderfactory.newinstance();          documentbuilder documentbuilder = documentbuilderfactory.newdocumentbuilder();          document document = documentbuilder.parse("botdata.xml");          node botdata = document.getelementsbytagname("botdata").item(0);          nodelist nodes = botdata.getchildnodes();          (int = 0; < nodes.getlength(); i++) {              node element = nodes.item(i);              if ("lolcat".equals(element.getnodename())) {                 element.settextcontent(changelolcat);             }           }          transformerfactory transformerfactory = transformerfactory.newinstance();          transformer transformer = transformerfactory.newtransformer();         domsource domsource = new domsource(document);          streamresult streamresult = new streamresult(new file("botdata.xml"));         transformer.transform(domsource, streamresult);     }catch (parserconfigurationexception pce) {         pce.printstacktrace();     } catch (transformerexception tfe) {         tfe.printstacktrace();     } catch (ioexception ioe) {         ioe.printstacktrace();     } catch (saxexception sae) {         sae.printstacktrace();     } 

after string changelolcat = message.substring(14);

edit: figured out how parse xml send what's in node message, right? feel i'm not supposed keep copying doc builder on , on in different methods

        if (message.equalsignorecase("!lolcat")) {     try {     documentbuilderfactory documentbuilderfactory =                       documentbuilderfactory.newinstance();      documentbuilder documentbuilder =                             documentbuilderfactory.newdocumentbuilder();      document document = documentbuilder.parse(botxml);                 xpathfactory xpathfactory = xpathfactory.newinstance();     xpath xpath = xpathfactory.newxpath();                 string lolcat = xpath.evaluate("//lolcat", document);      sendmessage(channel, sender + lolcat);     } catch (exception e) {                 e.printstacktrace();      }  } 

one way use javax.xml.parsers.documentbuilder parse xml file javax.swing.text.document, provides great interface getting , modifying individual elements , values. can write modified document original file, overwriting new version.

here links relevant javadocs:

you might want @ javadocs documentbuilderfactory object, best way generate documentbuilders.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -