Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. filenotfoundexception; </P> <p> Import javax. XML. parsers. documentbuilder; <br/> Import javax. XML. parsers. documentbuilderfactory; <br/> Import javax. XML. parsers. parserconfigurationexception; <br/> Import javax. XML. transform. transformer; <br/> Import javax. XML. transform. transformerexception; <br/> Import javax. XML. transform. Transformerfactory; <br/> Import javax. XML. transform. dom. domsource; <br/> Import javax. XML. transform. stream. streamresult; <br/> Import javax. XML. XPath. XPath; <br/> Import javax. XML. XPath. xpathconstants; <br/> Import javax. XML. XPath. xpathexpressionexception; <br/> Import javax. XML. XPath. xpathfactory; </P> <p> Import Org. w3C. dom. document; <br/> Import Org. w3C. dom. element; <br/> Import Org. XML. sax. saxexception; </P> <p>/** <br/> * obtain and set the time based on the imported XML file. <br/> * {talendtypes} string <br/> * {category} user defined <br/> * {Param} string ("xmlfile ") input: the string need to be printed. <br/> * {example} helloexemple ("world") # Hello world !. <Br/> */<br/> Public static string getsettime (string xiangmu) <br/> throws parserconfigurationexception, saxexception, <br/> ioexception, xpathexpressionexception, transformerexception, filenotfoundexception <br/>{</P> <p> string XFile = "myxml/time. XML "; <br/>/* // It can only be similar to the following <SPAN class = 'wp _ keywordlink '> Code </span>, the parameter xiangmu passed in is the following id value, and the time item can have multiple rows <br/> <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <root> <br/> <time Id = "lvguan" Now = "09:13:51"/> <br/> </root> <br/> */ </P> <p> simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: MM: SS "); <br/> string xintime = dateformat. format (new date (); </P> <p> documentbuilderfactory factory = documentbuilderfactory. newinstance (); <br/> xpathfactory = xpathfactory. newinstance (); <br/> XPath = xpathfactory. newxpath (); </ P> <p> factory. setignoringelementcontentwhitespace (true); <br/> documentbuilder DB = factory. newdocumentbuilder (); <br/> document xmldoc = dB. parse (new file (XFile); <br/> xpathfactory = xpathfactory. newinstance (); <br/> XPath = xpathfactory. newxpath (); <br/> element thebook = (element) XPath. evaluate ("/root/time [@ ID = '" + xiangmu + "']", xmldoc, xpathconstants. node); </P> <p> // The following rows are mainly used to change the project content. <br/> // thebook. getel Ementsbytagname ("now"). Item (0). settextcontent ("15"); // getelementsbytagname returns nodelist, so keep up with item (0 ). In addition, getelementsbytagname ("price") is equivalent to ". // price" of xpath ". </P> <p> string laotime = thebook. getattribute ("now"); <br/> thebook. setattribute ("now", xintime); </P> <p> // Save the modified XML file <br/> transformerfactory transfactory = transformerfactory. newinstance (); <br/> transformer = transfactory. newtransformer (); <br/> transformer. setoutputproperty ("indent", "yes"); <br/> domsource source = new domsource (); <br/> source. setnode (xmldoc); <br/> streamresult result = new streamresult (); <br/> result. setoutputstream (New fileoutputstream (XFile); <br/> transformer. transform (source, result); </P> <p> return laotime; </P> <p>}