In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to dynamically create and modify Spring bean configuration file, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Today, I was going to write the second article in the Spring series, but I suddenly remembered that I had forgotten to learn how to operate XML with java all the time. It was so wrong to think of such an important thing for so long, so today I will practice using dom4j to operate XML.
In fact, the library dom4j is so convenient that it is almost no different or difficult to operate XML with C#, so paste two pieces of code first.
There are several key points:
1. If you just create a XML file, you only need to import dom4j-1.6.1.jar. The path is as follows:
Spring-framework-2.5.6\ lib\ dom4j\ dom4j-1.6.1.jar
If you need to read or modify, you need to import another file in this library:
Spring-framework-2.5.6\ lib\ dom4j\ jaxen-1.1-beta-7.jar
Otherwise, an error will be reported as follows:
Java.lang.NoClassDefFoundError: org/jaxen/JaxenException
...
...
...
2. Dom4j supports chain operation, which is very similar to jQuery. This makes it very convenient to create a XML file and the code structure looks clearer.
3. Learn to XPath.... Otherwise you will be very painful, but XPath is actually very simple, it should not take much time, can not stop you, ~
Action section:
Java code
Package com.iteye.bolide74.action; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter Public class MyDom4j {/ * dynamically create a bean configuration file containing the bean of HelloWorld and add the initial value * * / public void createXML (String xmlPath, String msg) throws IOException {Document XmlDoc = DocumentHelper.createDocument () XmlDoc.addDocType ("beans", "- / / SPRING//DTD BEAN//EN", "http://www.springframework.org/dtd/spring-beans.dtd"); / / first create the beans root node Element beansEle = XmlDoc.addElement (" beans ") / / Note: dom4j is Element beanHelloWorld = beansEle.addElement ("bean") .addAttribute ("id", "HelloWorld") .addAttribute ("class", "com.iteye.bolide74.action.HelloWorld") that supports chained operations like jQuery. Element propertyHelloWorld = beanHelloWorld.addElement ("property") .addAttribute ("name", "msg"); Element valueHelloWorld = propertyHelloWorld.addElement ("value") .addText (msg); XMLWriter outXml = new XMLWriter (new FileWriter (new File (xmlPath); outXml.write (XmlDoc); outXml.close () } / * first iterate through all the bean in a bean configuration file to get the values of id and class, and then modify the msg value of HelloWorld this bean * @ throws IOException * * / public void editXML (String xmlPath, String msg) throws DocumentException, IOException {Document XmlDoc = new SAXReader (). Read (new File (xmlPath)) List xmlList = XmlDoc.selectNodes ("/ beans/bean"); System.out.println ("\ r\ nTraverse all bean to get id and class:"); for (Element element: xmlList) {System.out.println ("id:" + element.attributeValue ("id") + "/ class:" + element.attributeValue ("class")) } System.out.println ("\ r\ ndynamically modify the msg value of HelloWorld bean:"); / / use XPath to get a single node Node valueHelloWorld = XmlDoc .selectSingleNode ("/ beans/bean [@ id='HelloWorld'] / property [@ name='msg'] / value"); System.out.println ("original value is:" + valueHelloWorld.getText ()) ValueHelloWorld.setText (msg); System.out.println ("modified value is:" + valueHelloWorld.getText ()); / / remember to save after modification, otherwise you will wonder why the XML file has not changed, XMLWriter outXml = new XMLWriter (new FileWriter (new File (xmlPath); outXml.write (XmlDoc); outXml.close () }}
Java code
Package com.iteye.bolide74.action; public class HelloWorld {public String msg; public String getMsg () {return msg;} public void setMsg (String msg) {this.msg = msg;}}
The Tester implementation class section:
Java code
Package com.iteye.bolide74.tester; import java.io.IOException; import org.dom4j.DocumentException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.iteye.bolide74.action.HelloWorld; import com.iteye.bolide74.action.MyDom4j Public class HelloWorldTester {public static void main (String [] args) {String xmlPath = "/ WebContent/WEB-INF/conf/config_dom4j.xml"; MyDom4j myBeans = new MyDom4j (); try {myBeans.createXML (System.getProperty ("user.dir") + xmlPath, "Hellomine worldview this is created by dom4j!") } catch (IOException e) {e.printStackTrace ();} ApplicationContext ac = new FileSystemXmlApplicationContext (xmlPath); HelloWorld helloWorld = (HelloWorld) ac.getBean ("HelloWorld"); System.out.println (helloWorld.getMsg ()) Try {myBeans.editXML (System.getProperty ("user.dir") + xmlPath, "Hellograd worldview this is edited by dom4j!");} catch (DocumentException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () } / / re-get bean configuration file ac = new FileSystemXmlApplicationContext (xmlPath); helloWorld = (HelloWorld) ac.getBean ("HelloWorld"); System.out.println ("\ r\ n" + helloWorld.getMsg ());}}
The output is as follows:
Html code
Hello,world!this is created by dom4j! Go through all the bean to get id and class: id:HelloWorld / class:com.iteye.bolide74.action.HelloWorld dynamically modify the msg value of the bean HelloWorld: the original value is: Hellograd worldview this is created by dom4j! The modified value is: Hellojime worldview this is edited by dom4j! Hello,world!this is edited by dom4j! On how to dynamically create and modify Spring bean configuration files to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.