Using Jdom to bring two XML merges to a Document object

Source: Internet
Author: User
Tags definition config readable xpath

Typically, some application frameworks are configured with XML, and many support multiple XML files, such as the Struts framework can configure multiple Struts-config-xxx.xml files, and Spring also allows you to use multiple applicationcontext-xxx. XML files, such as DWR, can also be configured by more than one dwr-xxx.xml by function or by other means. We know that such multiple XML have the same canonical definition, so how does the program parse them together? I have seen Actionservlet is the struts-config-xxx.xml of the individual resolution. One approach I've introduced here is to synthesize a document with XML that has the same canonical definition and then process the document object, such as XPath lookup and DOM object manipulation, without having to go through multiple document searches every time.

As we all know, there are two ways to manipulate XML, Dom:xml mapped in memory to a tree; SAX: an event-based approach. Common XML Java parsing components are dom4j (Apache), JDOM, and JAXP (Sun's), all of which provide DOM and SAX implementations and Xpath lookups.

Here I will introduce the use of JDOM to complete a combination of multiple XML files to get a Document object, the implementation code is as follows:

/*
* Created on Sep 23, 2007
*/
Package Com.unmi;
Import java.io.IOException;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import org.jdom.JDOMException;
Import Org.jdom.input.SAXBuilder;
/**
* Combine two or more XML together as one document
* I put this operation into the main method.
* @author Unmi
*/
public class Combinexml
{
/**
* @param args
* @throws Exception
*/
public static void Main (string[] args) throws Exception
{
Document document = NULL;
try {
Saxbuilder dbf = new Saxbuilder ();
Document = (document) Dbf.build (Args[0]);
Element docroot = Document.getrootelement ();
for (int i = 1; i < args.length; i++) {
Document Tmpdoc = Dbf.build (Args[i]);
list<element> NLT = Tmpdoc.getrootelement (). GetChildren ();
for (int j = 0; J < nlt.size ();) {
Element el = nlt.get (0);
Get free Element
El.detach ();
Docroot.addcontent (EL);
}
}
catch (IOException e) {
throw new Exception ("File not readable.");
catch (Jdomexception e) {
throw new Exception ("File parsed error.");
}
TODO, you can process this document now.
}
}
/*
* Created on Sep 23, 2007
*/
Package Com.unmi;
Import java.io.IOException;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import org.jdom.JDOMException;
Import Org.jdom.input.SAXBuilder;
/**
* Combine two or more XML together as one document
* I put this operation into the main method.
* @author Unmi
*/
public class Combinexml
{
/**
* @param args
* @throws Exception
*/
public static void Main (string[] args) throws Exception
{
Document document = NULL;
try {
Saxbuilder dbf = new Saxbuilder ();
Document = (document) Dbf.build (Args[0]);
Element docroot = Document.getrootelement ();
for (int i = 1; i < args.length; i++) {
Document Tmpdoc = Dbf.build (Args[i]);
list<element> NLT = Tmpdoc.getrootelement (). GetChildren ();
for (int j = 0; J < nlt.size ();) {
Element el = nlt.get (0);
Get free Element
El.detach ();
Docroot.addcontent (EL);
}
}
catch (IOException e) {
throw new Exception ("File not readable.");
catch (Jdomexception e) {
throw new Exception ("File parsed error.");
}
TODO, you can process this document now.
}
}

The purpose of this study is that I want to do a tool to merge multiple struts-config profiles and then enter an attribute value in Nath, Nction, Norward, name, or type to find other property values associated with it, which can facilitate program debugging, such as seeing which Aciton.do knows which pages to turn to, which formbean to attach to, and what other related attribute values are.

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.