Home Backend Development XML/RSS Tutorial How to use XML to implement the structure of a multi-channel access website

How to use XML to implement the structure of a multi-channel access website

Feb 27, 2017 pm 04:22 PM

1. Background

In today’s websites, there are more and more access channels, and the technology is becoming more and more advanced, such as WAP, SMS, EMAIL, traditional Web, Socket, etc. If even the database and LDAP are considered to be connected, then the space that needs to be expanded in the design must be very good to ensure that when adding new channels, no more code modifications or even code changes are required. . But is it possible? I don’t think it is possible, but is there any way to better solve the perfection of this multi-channel access framework?

2. Architecture

How to use XML to implement the structure of a multi-channel access website

[Figure 1]

As shown in Figure 1, when all the existing accesses have been used, the designers are dazzled. If it is to get a share, You can write these programs in any way, and they can definitely be implemented, but it will be more painful to maintain. Let’s go back to the question, how can we achieve a more perfect implementation? Figure 2 shows:

How to use XML to implement the structure of a multi-channel access website

[Picture 2]

Picture 2 looks like an eight-clawed octopus. The octopus legs are connected to all access channels respectively. The core of connecting all these channels is the octopus head xmlRouter. The role of the Router here is to communicate across all channels, implement data routing, and enhance the scalability and flexibility of the system's architecture. The benefits will be many. It is called XMLRouter because if XML, a flexible and standardized language, is not used as a data transmission medium, the workload of the Router will also increase exponentially. Defining the XML specification will bring about future expansion. Comes with many benefits.

3. Ideas and Patterns

The original idea of ​​XMLRouter came from the Builder Pattern in the computer motherboard and . The PCI slot of the computer motherboard defines the PCI specification. As long as The card you produce complies with the PCI standard, then you can plug it into the motherboard and it will work. As for how it works inside, it has been encapsulated. The Builder Pattern proposes to separate complex construction and implement it step by step. .XMLRouter separates these complex channels and performs them one by one.

Services idea: In order to communicate with Router, a unified interface must be defined when these channels are connected, which is called Services. As long as the program conforms to the Services specification, it can access the Router and route data.

Factory mode and Composite mode

XMLRouter will be generated using the Factory mode in the actual design, and the Router is generated by the RouterFactory Production will be placed in the queue when it is put into use. The corresponding Router for transmitting data, receiving data and returning data is called from the queue, and the Composite mode is applied.

IV. XML configuration file

The XML file is divided into two parts for use in Router. The first one is the configuration of Router, such as:

The following is a reference fragment:

<?xml version="1.0" ?> 
<services> 
   <!-- database Service --> 
   <service name="database" type="database" class="com.web.service.DBService"> 
   <connector 
       driver="com.microsoft.jdbc.sqlserver.SQLServerDriver" 
       url="jdbc:microsoft:sqlserver://192.168.0.179:1433" user="test" 
       passwd="test" /> 
   </service> 
   <!-- Web Service--> 
   <service name="web" type="web" class="com.web.service.WebService" > 
     <connector /> 
   </service> 
…… 
</services>
Copy after login


This is the configuration file of Router. The service node represents the channel that needs to be accessed. The service node contains connector sub-nodes. The configuration of the sub-nodes is distinguished according to type. If it is database, then Contains attributes such as url, user, passwd, driver, etc. If it is a socket, it includes attributes such as port, maxthread, etc. The attribute values ​​​​can be configured according to your own definition.

Another XML file is the XML transaction data file. Used to transfer data in all services. Each Service contains a corresponding XML file. For example, the format of webtrans.xml is as follows:

The following is a reference fragment:

<?xml version="1.0" ?> 
<transaction> 
   <trans name="addDoc" service="database" method="insert"> 
     <PRoperty name="createtime" type="timestamp"/> 
     <property name="creatorid" type="long"/> 
     <property name="doctypeid" type="int"/> 
     <property name="docstatusid" type="int"/>       
   </trans>     
</transaction>
Copy after login


The corresponding dbtrans.xml format is as follows


The following is a quotation fragment:

<trans name="addDoc" table="TDOC_DOCS" method="insert"> 
     <primarykey name="docid" />     
     <set> 
       <property name="createtime" type="timestamp"/> 
       <property name="creatorid" type="long"/> 
       <property name="doctypeid" type="int"/> 
       <property name="docstatusid" type="int"/>       
     </set> 
   </trans>
Copy after login



The rest of the XML can be customized according to such rules

5. Technical implementation

RouterFactory

The following is a quote:

package com.web.router; 
import com.web.platform.Exception.RouterException; 
import java.util.java/util/Hashtable.java.html" target="_blank">Hashtable;
Copy after login




The following is a quotation fragment:

/** 
 * Router产生和清除的类 
 */ 
public class RouterFactory  
{ 
  /** 
   * Router存储的树front 
   */ 
  private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairFront = null; 
  /** 
   * Router存储的树back 
   */ 
  private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairBack = null;  
/** 
   * Router存储的树 
   */ 
  private static java/util/Hashtable.java.html" target="_blank">Hashtable QueueRouter = null;  
  /** 
   * 返回的XMLRouter 
   */ 
  public static XMLRouter instance = null; 
  /** 
   * Router的定义 
   */ 
  public static RouterDefine routerdefine = null; 
  /** 
   * Router的ID号 
   */ 
  public static long routeIndex = 0; 
  /** 
   * @roseuid 3F169C21027C 
   */ 
  public RouterFactory()  
  { 
  } 
  /** 
   * 初始化Hashtable和Vector 
   */ 
  public static void initFactory() throws java/lang/Exception.java.html" target="_blank">Exception 
  { 
      QueuePairFront = new java/util/Hashtable.java.html" target="_blank">Hashtable();  
      QueuePairBack = new java/util/Hashtable.java.html" target="_blank">Hashtable();  
      QueueRouter    = new java/util/Hashtable.java.html" target="_blank">Hashtable(); 
      initRouteDefine(); 
  }  
/** 
   * 初始化Route的设置 
   *  
   */ 
  private static void initRouteDefine() throws java/lang/Exception.java.html" target="_blank">Exception 
  { 
      if( routerdefine == null ) 
        routerdefine = new RouterDefine(); 
      routerdefine.loadRouterDef(); 
  } 
  /** 
   * 返回实例 
   * @return com.web.router.XMLRouter 
   */ 
  public static XMLRouter getInstance(long index) throws RouterException 
  { 
     return (XMLRouter)QueueRouter.get(new java/lang/Long.java.html" target="_blank">Long(index)); 
  }  
  /** 
   * 产生一个XMLRouter的实例 
   * @return com.web.router.XMLRouter 
   * @roseuid 3F1618A103BC 
   */ 
  public static XMLRouter popInstance() throws RouterException 
  { 
      routeIndex ++; 
      instance = new XMLRouter(routeIndex); 
      setDefine( instance ); 
      QueueRouter.put(new java/lang/Long.java.html" target="_blank">Long(routeIndex), instance);      
     return instance; 
  } 
  /** 
   * 清空Hashtable,Vector等 
   * @roseuid 3F1618B203C1 
   */ 
  private static void freeResource() throws java/lang/Exception.java.html" target="_blank">Exception 
  { 
      QueuePairFront.clear(); 
      QueuePairBack.clear(); 
      QueueRouter.clear(); 
      QueuePairFront = QueuePairBack = QueueRouter = null; 
} 
  /** 
   * 清除实例 
   * @param instanceID 
   * @throws Exception 
   */ 
  public static void removeInstance(XMLRouter instance) throws java/lang/Exception.java.html" target="_blank">Exception  
  { 
      instance.clear(); 
     QueueRouter.remove( new java/lang/Long.java.html" target="_blank">Long(instance.getIndex() ) ) ; 
  } 
/** 
 * Method isNull. 
 * @return boolean 
 */ 
  public static boolean isNull() 
  { 
     ……  
      return false; 
  } 
}
Copy after login



XMLRouter

The following is a quotation fragment:

package com.web.router;  
import com.web.platform.Exception.RouterException; 
import com.web.common.*; 
import java.util.*; 
import java.lang.reflect.java/lang/reflect/Method.java.html" target="_blank">Method; 
import java.lang.reflect.java/lang/reflect/Constructor.java.html" target="_blank">Constructor;  
/** 
 * @author keli 
 * @version 0.0.1 
 * 平台的关键,路由的类,每个Router将从RouterFactory里读取 
 * Router存储的树front,和back,routeIndex,目的是为了能在路由 
 * 之后可以清除申请的对象。 
 * Router可以实现同步和异步的功能. 
 */ 
public class XMLRouter  
{ 
  /** 
   * Router存储的树front 
   */ 
   private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairFront = null; 
  /** 
   * Router存储的树back 
   */ 
   private static java/util/Hashtable.java.html" target="_blank">Hashtable QueuePairBack = null;  
/** 
   * 本router的index号码 
   */ 
   private long routeIndex = 0;  
/** 
   * router的设置 
   */ 
   private RouterDefine define = null;   
/** 
   * 用于判断是路由的起回点 
   */ 
   private java/lang/String.java.html" target="_blank">String action = ""; 
   /** 
   *此变量只是用于在routeto方法中申请新的class  
   */ 
   private java/lang/String.java.html" target="_blank">String classname = ""; 
    /** 
   */ 
    public XMLRouter(long index)  
    { 
     routeIndex = index; 
    } 
    /** 
   * 路由 
   * @throws Exception 
   * @roseuid 3F1616BD0186 
   */ 
    public void routing(Env env) throws RouterException, java/lang/Exception.java.html" target="_blank">Exception  
    { 
      /*如果为起点*/ 
     if( action.equalsIgnoreCase( RouterConstant.CFG_FUNC_ROUTETO ) )   
     { 
       …… 
     } 
     /*如果为返回点*/ 
     else if( action.equalsIgnoreCase( RouterConstant.CFG_FUNC_ROUTEBACK ) ) 
     { 
     …… 
     } 
     /*否则为错误*/ 
     else 
       throw new RouterException("Set Router action error."); 
    } 
   /** 
   * 读取本Router的id号. 
   * @return long 
   */ 
   public long getIndex() 
   { 
      return routeIndex;     
   } 
/** 
   * 清除所有对象. 
   * @throws RouterException 
   */ 
   public void clear() throws RouterException 
   { 
     QueuePairFront.remove(new java/lang/Long.java.html" target="_blank">Long(routeIndex)); 
     QueuePairBack.remove(new java/lang/Long.java.html" target="_blank">Long(routeIndex)); 
     /*系统回收*/ 
     java/lang/System.java.html" target="_blank">System.runFinalization();      
   }  
   /** 
   * 设置本Router的设置. 
   * @param def 
   * @throws RouterException 
   */ 
   public void setDefine(RouterDefine def) throws RouterException 
   { 
     define = def;   
   }  
   /** 
   * 设置action的值 
   * @param actionName 
   * @throws RouterException 
   */ 
   public void setAction( java/lang/String.java.html" target="_blank">String actionName ) 
   { 
     action = actionName;     
   } 
}
Copy after login



Service class

The following is a reference fragment:

package com.web.common;  
import com.web.platform.Exception.RouterException;  
/** 
 * Service的父类,abstract  
 */ 
public abstract class RouteService  
{ 
  /** 
   */ 
  public RouteService()  
  { 
  } 
  /** 
   * routeTo方法,是交易的起点。 
   * @param env 
   * @throws RouterException 
   */ 
  public abstract void routeto(Env env) throws RouterException; 
  /** 
   * routeBack,交易的结束点, 
   * @param env 
   * @throws RouterException 
   */ 
  public abstract void routeback(Env env) throws RouterException; 
  /** 
   * routeaccept方法,是交易的接收点,也是routeto的接收函数, 
   * routeaccept为被动交易对象的主要处理函数 
   * @param env 
   * @throws RouterException 
   */ 
  public abstract void routeaccept(Env env) throws RouterException;   
  /** 
   * routing方法,是Service对外的接口函数 
   * @throws RouterException 
   */ 
  public abstract void routing() throws RouterException;
Copy after login


Next, you need to implement all the Services classes, which will not be introduced here.

6. Description

This Router so far It can only implement synchronous transactions and does not support asynchronous transactions for the time being. However, because the Router is designed using the Composite model, asynchronous transactions can also be expanded. I will not do a detailed analysis here.

The above is how to use it. XML realizes the content of the structure of multi-channel access to the website. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles