Use struts digester to parse XML files

Source: Internet
Author: User
Topic: Use struts digester to parse XML files
Author: Cai Yi
Time: 2005-5-10

Apache commons digester is used to simplify XML file processing. It depends on the components beanutils, logging, and collections.
Internally, we use SAX to parse XML files.

This solution is used to repair the breadcrumb In the ABC project. The so-called breadcrumb is the connection that plays a navigation role on the page.
For example, "document Category Management-> repair Management-> standard document category ". Here we use ct-views.xml files to store bread chips
Content. Part of the XML file is as follows:
<Views>
<Page id = "vsd_wi003">
<Description> standard document list </description>
<Helpurl> standarddoc/workitem/sortworkitemlist.htm <Crumb level = "0">
<Name> standard document </Name>
</Crumb>
<Crumb level = "1">
<Name> document preparation </Name>
<URL>/standarddoc/workitemindexaction. DO </URL>
<Target> mainworkframe </Target>
</Crumb>
<Crumb level = "2">
<Name> document list </Name>
</Crumb>
</Page>
</Views>

We have defined three common JavaBean, which are viewconfig, pageconfig, and crumbconfig in sequence from large to small.
They include their own setter and getter. View, and page contains crumb. Read the stack once during system loading.
.

The class used to parse XML is parseviewconfig, which is used to parse the content in CT-views and buffer it into the stack.
The content is as follows:

Public class parseviewconfig {
Digester = new digester ();
Logger logger = toolkit. getinstance (). getlogger (this. getclass (). getname ());
Public viewconfig parse (inputstream input ){
Viewconfig Config = NULL;
// Do not verify the validity of XML and the corresponding DTD
Digester. setvalidating (false );
// Create a viewconfig object when you encounter <views>
Digester. addobjectcreate ("views", viewconfig. Class );
// Create a pageconfig object when you encounter <page>
Digester. addobjectcreate ("views/page", pageconfig. Class );
// Set attribute
Digester. addsetproperties ("views/page", "ID", "ID ");
Digester. addsetproperties ("views/page", "file", "file ");
Digester. addsetproperties ("views/page", "path", "path ");
// Set bean Property
Digester. addbeanpropertysetter ("views/page/description", "Description ");
Digester. addbeanpropertysetter ("views/page/helpurl", "helpurl ");
// Create a crumbconfig object when you encounter <crumb>
Digester. addobjectcreate ("views/page/crumb", crumbconfig. Class );
// Set attribute
Digester. addsetproperties ("views/page/crumb", "level", "level ");
// Set bean Property
Digester. addbeanpropertysetter ("views/page/crumb/url", "url ");
Digester. addbeanpropertysetter ("views/page/crumb/Name", "name ");
Digester. addbeanpropertysetter ("views/page/crumb/target", "target ");
// Here addcrumbconfig is the method name for calling the parent contact object, and here it is pageconfig
Digester. addsetnext ("views/page/crumb", "addcrumbconfig ");
// Here addpageconfig is the method name for calling the parent contact object, and here it is viewconfig
Digester. addsetnext ("views/page", "addpageconfig ");

Try {
Config = (viewconfig) digester. parse (input );
} Catch (saxexception ex ){
Logger. Error (ex. getmessage (), Ex );
} Catch (ioexception ex ){
Logger. Error (ex. getmessage (), Ex );
}
Return config;
}
This file returns a reference to the viewconfig object. Then we use the plug-in technology to load the information before loading
This is added to the sysinitplugin class, which implements the org. Apache. Struts. Action. plugin interface.
The code for parsing CT-views is as follows:
// Get the file stream
Inputstream input = actionservlet. getservletcontext (). getresourceasstream (
"/WEB-INF/ct-views.xml ");
// Parse the file
Parseviewconfig = new parseviewconfig ();
Viewconfig Config = parseviewconfig. parse (input );

Therefore, you can use the getter method on the page to obtain the value of viewconfig.

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.