Wrote a Java XML file to read the tool class

Source: Internet
Author: User
Tags object net return string version log4j
XML if we have one of the following XML files, we can think of it as a table in the relation database, which is a relational table named managers, and a <manager> contains a record ( Record), and the service and implementation in the manager are a field!











Managers table
+ Service Implementation
Net.csdn.blog.xport.IDBConnectionManager Net.csdn.blog.xport.impl.DBConnectionManagerCommonImpl
Net.csdn.blog.xport.IDomainIdentify Net.csdn.blog.xport.impl.DomainIdentifyImpl

So, in a program, you can read the attribute value on the XML node (node) in a way that looks like table query!

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE managers SYSTEM "MANAGER-CONFIG.DTD" >
<managers>
name= "Dbconnectionmanager" >
<service>net.csdn.blog.xport.IDBConnectionManager</service>
<implementation>net.csdn.blog.xport.impl.DBConnectionManagerCommonImpl</implementation>
</manager>
<manager name= "Ntaccountidendify" >
<service>net.csdn.blog.xport.IDomainIdentify</service>
<implementation>net.csdn.blog.xport.impl.DomainIdentifyImpl</implementation>
</manager>
</managers>

Read in the following code:

Lookup with Manager as tag tag, property with name and attribute value of "Dbconnectionmanager"
XML node, and returns the DOM element object for this node!
Similar to the Name field PK value, and name= "Dbconnectionmanager"
Managerelement
= Xmlfileutil.findelement (Xmldomcontent.getrootelement (),
"Manager",
"Name",
"Dbconnectionmanager");
if (managerelement!= null) {
Gets the Implementation Property object in the element ...
Serviceimplelement = managerelement.element ("Implementation");
if (serviceimplelement!= null) {
Take the value of this property ...
serviceimplclassstring = Serviceimplelement.gettext ();
}
}

Source of XML Utility Class:

/*

* @ (#) Xmlfileutil.java 1.0 2004/12/20
*
* Copyright Shark Wang, All rights reserved.

*/

Package net.csdn.blog.xport;

Import Java.net.URL;
Import Java.util.Iterator;

Import Org.apache.log4j.LogManager;
Import Org.apache.log4j.Logger;
Import Org.dom4j.Attribute;
Import org.dom4j.Document;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;

/**
* The <code>XmlUtil</code> class supported your code to read/write XML
* Data from the file. All methods in this class depend on <code>dom4j</code>.
*
* @author Shark Wang
* @version 1.0, 2004/12/20
* @since Tutorial 1.0
*/
public class Xmlfileutil {

private static Logger Logger = Logmanager.getlogger (Xmlfileutil.class);

/**
* Read XML content from some file, and load XML data into the
* Document object.
*
* @param filePath String
* @return Document
*/
public static Document Loadxmlfile (String filePath) {

/* Marked by Shark Wang
*****************************************************************
Get resolver to ignore the DTD validation
Entityresolver resolver = new Entityresolver () {
Public InputSource resolveentity (String publicid,
String SystemID) {
return new InputSource (New StringBufferInputStream (""));
}
};
Create reader
Saxreader reader = new Saxreader ();
Set reader attribute to ignore DTD validation
Reader.setentityresolver (resolver);
Reader.setvalidation (FALSE);
Reader.setincludeexternaldtddeclarations (FALSE);
Reader.setincludeinternaldtddeclarations (FALSE);
*******************************************************************
*/
Saxreader reader = new Saxreader ();
Try to load XML data into Document object
Document doc = null;
try {
String urlstring = null;
if (Filepath.startswith ("/")) {
URLString = "file://" + filePath;
}
else {
URLString = "file:///" + filePath;
}
Logger.debug ("XML File ' s URL: + urlstring);"
doc = Reader.read (new URL (urlstring));
}
catch (Exception ex) {
Logger.info ("Can not load" + FilePath);
Logger.debug (Ex.getmessage (), ex);
}
Return Document Object
return doc;
}

/**
* Get attribute value by name to some XML element.
*
* @param element element
* @param attributename String
* @return String
*/
public static String Getattributevalue (element element,
String AttributeName) {
String attributevalue = null;
for (Iterator i = Element.attributeiterator (); I.hasnext ();) {
attribute attribute = (attribute) i.next ();
if (Attribute.getname (). Equals (AttributeName)) {
AttributeValue = (String) attribute.getdata ();
Break
}
}
return attributevalue;
}

public static element Findelement (element searchedelement,
String Targetnodeprefix,
String Targetnodeattributename,
String targetnodeattributevalue) {
Element elementtarget = null;
for (Iterator i = Searchedelement.elementiterator (Targetnodeprefix);
I.hasnext (); ) {
element element = (Element) I.next ();
String Strmanagername =
Xmlfileutil.getattributevalue (element,
Targetnodeattributename);
if (Strmanagername.equals (Targetnodeattributevalue)) {
Elementtarget = element;
Break
}
}
return elementtarget;
}

Private Xmlfileutil () {
}
}


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.