什么是XML?
前言
21年年底,笔者刚开始接触Android开发,在菜鸟教程中看教程学习写Android的前端。当时前端界面是使用.xml的文件编写,因为之前在学习springboot的aop时就有创建.xml文件进行配置,在Android又碰到了xml,于是就产生了xml是什么的疑问,但因为时间着急也没有去深究xml是什么就上手用了。最近,在复习前端知识的时候,正好看到了xml,于是,下面是个人从网上学习并结合自己的理解写下的关于xml的介绍,如有不足或错误之处请指正,谢谢。
XML简介
XML英文全称:EXtensible Markup Language,即可扩展标记语言。它是W3C组织的推荐标准。
标记语言
它是一种标记语言,类似HTML,都是以标签的形式形式包裹内容
如:
- HTML写一个标题是:
<h1>一级标题</h1>
- XML写一个标题是:
<headTitle>一级标题</headTitle>
从上面的例示可以看到,html和xml的标题标签都是有前后的尖括号标记信息内容,后面的尖括号要有斜杠’/'来封闭。
自定义标签
我们也可以看到上面xml的标题我是用<headTitle>
来标记的,和html的不一样,这就引出了xml的另一个特点:XML标签没有被预定义。我们可以自行定义标签。如我们也可以自定义别的标题标签名称,叫<titleA>
、<titleB>
等等,而在HTML中只能使用HTML标准定义的标签,如<h1>
、<h2>
、<p>
等等。
传输和存储数据
在前端,我们常用的两个数据交换格式1分别是XML和JSON。这就又引出了XML的一个重要的特点:XML是被设计用来传输和存储数据的。
- 显示信息:不同于HTML的标签是用来描述页面内容,展示给用户看的;
- 传输信息:XML的标签是用来传输和存储数据,它是数据的载体。
- XML不可代替HTML:XML不是HTML的代替,它是HTML的补充。数据存储可以存储在独立的XML中,HTML专注与布局和显示
如上面写的标题例子,我们写的HTML的标题就是展示给用户看“一级标题”;而我们自定义的XML标题就是客户端和服务端说“我有一个叫headTitle的数据,内容是一级标题”,类似定义了一个变量headTitle=“一级标题”。
XML声明
XML文件的声明语法如下:
<?xml version="1.0" encoding="UTF-8"?>
包含属性:
- 版本:当前xml文档使用的版本
- 编码:当前xml文档使用的编码方式
常用作配置文件
像笔者前言所说的,笔者在写springboot的aop配置和Android的界面控件都用到了.xml的文件进行编写。XML文件常被用作为配置文件,如下在.xml文件配置aop的代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 被代理的目标对象-->
<bean id="gcd" class="com.website.trial1.services.impl.GcdService"/>
<bean id="lcm" class="com.website.trial1.services.impl.LcmService"/>
<bean id="min" class="com.website.trial1.services.impl.MinusService"/>
<bean id="addService" class="com.website.trial1.services.impl.AddService"/>
<bean id="divide" class="com.website.trial1.services.impl.DivideService"/>
<bean id="multi" class="com.website.trial1.services.impl.MultiService"/>
<!-- 通知-->
<bean id="serviceAdvice" class="com.website.trial1.aop.ServiceAdvice"/>
<!-- AOP配置-->
<!-- proxy-target-class属性表示被代理的类是否为一个没有实现接口的类,Spring会依据实现了接口则使用JDK内置的动态代理,如果未实现接口则使用cblib -->
<aop:config proxy-target-class="false">
<!--切面配置-->
<!--ref表示通知对象的引用-->
<aop:aspect ref="serviceAdvice">
<!--配置切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.website.trial1.services.Calculator.*(..)))"/>
<!--声明通知,method指定通知类型,pointcut指定切点-->
<aop:before method="doBefore" pointcut-ref="pointcut"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="pointcut" returning="retObj"/>
<aop:around method="around" pointcut-ref="pointcut"/>
<aop:after method="doAfter" pointcut-ref="pointcut"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="pointcut" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>
又如Android配置登录页面组件的.xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--返回箭头-->
<com.example.myapplication4.FontIconView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="33dp"
android:text="@string/returnIcon"
android:textSize="32dp" />
<!--注册按钮-->
<TextView
android:id="@+id/loginToRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginTop="33dp"
android:gravity="center"
android:text="@string/register"
android:textColor="@color/gray"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<!--欢迎文本-->
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/black"
android:textSize="22sp"
android:text="@string/welcome"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1"
>
<!--用户名-->
<EditText
android:id="@+id/loginUsername"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true"
android:hint="请输入用户名"/>
<!--密码-->
<EditText
android:id="@+id/loginPassword"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true"
android:hint="请输入密码"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_weight="10"
>
<!--登录-->
<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:background="@drawable/shape"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
其他
XML缺点
- XML数据体积大,传输效率低
- 在JavaScript中解析XML不方便
常用JSON作为数据传输格式
虽然xml也可以用作数据传输,但因为JSON的数据体积较小,与JavaScript交互更方便、更容易解析,传输速度也较快等原因,现在更流行用JSON作为数据传输。xml更多的是用作配置文件。
更多XML信息
更多XML详情信息,请阅读:XML官网或w3cschool中文官网的XML教程文档
数据交换格式:客户端和服务器之间进行数据传输和交换的格式 ↩︎