"Parsing packages with dom4j" CRUD Operations on XML in DOM manner

Source: Internet
Author: User

It Programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.

Package edu.dom4j.dom;

Import Java.io.FileOutputStream;

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.io.OutputFormat;

Import Org.dom4j.io.SAXReader;

Import Org.dom4j.io.XMLWriter;

Import Org.junit.Test;

public class Xmlcrud {

Private String xmlfile = "Webroot/product2.xml";

Get the Document object for Product2.xml documents

Public Document GetDocument () throws documentexception{

Saxreader reader = new Saxreader ();

Document document = Reader.read (xmlfile);

return document;

}

Writes an in-memory document object to an XML file

Public document Write2xml (document document) throws ioexception{

OutputFormat format = Outputformat.createprettyprint ();

Format.setencoding ("Utf-8");

XMLWriter writer = new XMLWriter (new FileOutputStream (xmlfile), format);

Writer.write (document);

Writer.close ();

return document;

}

Add New Tags <stock> (add to last) under the first <product>

@Test

public void Addnewtag () throws Documentexception, ioexception{

Document document = GetDocument ();

Element root = Document.getrootelement ();

Get the first product node

Element Product1 = root.element ("Product");

Add a new node to the PRODUCT1 node <stock>

Element stock = Product1.addelement ("Stock")

. AddAttribute ("description", "stock");

Stock.settext ("200");

Write2xml (document);

}

Insert a new label at the specified location: <notes> This is the wrench </notes> front Insert <salenum>

@Test

public void Insertnewtag () throws Documentexception, ioexception{

Document document = GetDocument ();

Creating a new node here does not create a new node in the way above, only with the help of Documenthelper

Element salenum = documenthelper.createelement ("Salenum");

Salenum.addattribute ("description", "Sales"). SetText ("100");

Get the first product node

Element Product1 =document.getrootelement (). Element ("Product");

Get the child node collection for the product node

list<element> sonlist = product1.elements ();

Sonlist.add (2,salenum);

Write2xml (document);

}

Delete the specified node: delete the <salenum> node inserted above

@Test

public void Removetag () throws Documentexception, ioexception{

Document document = GetDocument ();

Element product1 = Document.getrootelement (). Element ("Product");

Element salenum = product1.element ("Salenum");

Product1.remove (Salenum);

Write2xml (document);

}

Change <stock description= "inventory" >200</stock> properties and text

@Test

public void Updatetag () throws Documentexception, ioexception{

Document document = GetDocument ();

Element product1 = (Element) document.getrootelement (). Elements ("Product"). Get (0);

Element stock = Product1.element ("Stock");

Stock.setattributevalue ("description", "Inventory volume");//This method is obsolete

Stock.attribute ("description"). SetValue ("Stock Amount");

Stock.settext ("200 pieces");

Write2xml (document);

}

}




product2.xml File:

<?xml version= "1.0" encoding= "Utf-8"?>

<catalog id= "Cata1" >

<product category= "Handtool" inventory= "Instock" >

<specifications weight= "2.0kg" > Wrench </specifications>

<price street= "Hong Kong Street" >80.0</price>

<notes> This is the wrench </notes>

</product>

<product category= "Table" >

<specifications> Tables </specifications>

<price street= "Macau Street" wholesale= "part" >100.0</price>

</product>

</catalog>




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.