JSP Add, delete, read the content code of the specified tag in the XML file

Source: Internet
Author: User

Web effects p/jsp.html target=_blank >jsp Tutorial Add, delete, read the content code of the specified tag in the XML file

Package XML;

Import Java.io.file;
Import java.util.list;

Import org.dom4j.document;
Import org.dom4j.documentexception;
Import org.dom4j.element;
Import Org.dom4j.qname;
Import Org.dom4j.io.saxreader;

public class Dom4jtest {

Private Saxreader reader;
Private document document;
private static string path = "C:/dom4jsample1.xml";

Public dom4jtest (String str) throws Documentexception {
reader = new Saxreader ();
Document = Reader.read (new file (str));
}

public static void Main (string[] args) throws Documentexception {
Todo auto-generated method stub
Dom4jtest test = new Dom4jtest (path);
Test.getinfo ();

}

public void GetInfo () {
Element root = Document.getrootelement ();

QName QName = new QName ("IP");
list<element> list = root.elements (QName);

for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i). GetText ());
}

}

}

public void Add (string add) throws IOException {
Element root = Document.getrootelement ();
Element e = root.addelement ("IP");
E.settext (add);
WriteFile (document);
}

Write to File
private void WriteFile (document DOC) throws IOException {
FileWriter out = new FileWriter (path);
Doc.write (out);
Out.flush ();
Out.close ();
}

Related Article

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.