Talend is a class that is automatically extracted and changed to the latest time in combination with XML writing.

Source: Internet
Author: User
Tags dateformat xpath

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>}   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.