原文地址:攻城狮不是猫
第一种方法
首先拿到xml 例如:
<status><sigar cpu="31.2" memory="87" jvm="79" disk="70" /><jvm><memory free='98567840' total='124780544' max='1825046528'/><memorypool name='PS Eden Space' type='Heap memory' usageInit='32505856' usageCommitted='32505856' usageMax='673710080' usageUsed='2554416'/><memorypool name='PS Old Gen' type='Heap memory' usageInit='87031808' usageCommitted='87031808' usageMax='1368915968' usageUsed='18421456'/><memorypool name='PS Survivor Space' type='Heap memory' usageInit='5242880' usageCommitted='5242880' usageMax='5242880' usageUsed='5236832'/><memorypool name='Code Cache' type='Non-heap memory' usageInit='2555904' usageCommitted='8060928' usageMax='251658240' usageUsed='6854528'/><memorypool name='Compressed Class Space' type='Non-heap memory' usageInit='0' usageCommitted='2490368' usageMax='1073741824' usageUsed='2234352'/><memorypool name='Metaspace' type='Non-heap memory' usageInit='0' usageCommitted='20578304' usageMax='-1' usageUsed='19735120'/></jvm><connector name='"http-nio-8060"'><threadInfo maxThreads="200" currentThreadCount="10" currentThreadsBusy="1" /><requestInfo maxTime="249" processingTime="421" requestCount="3" errorCount="0" bytesReceived="0" bytesSent="23445" /><workers><worker stage="S" requestProcessingTime="736" requestBytesSent="0" requestBytesReceived="0" remoteAddr="0:0:0:0:0:0:0:1" virtualHost="localhost" method="GET" currentUri="/manager/status" currentQueryString="XML=true" protocol="HTTP/1.1" /></workers></connector><connector name='"ajp-nio-8061"'><threadInfo maxThreads="200" currentThreadCount="10" currentThreadsBusy="0" /><requestInfo maxTime="0" processingTime="0" requestCount="0" errorCount="0" bytesReceived="0" bytesSent="0" /><workers></workers></connector></status>
通过:http://www.xmlforasp.net/CodeBank/System_Xml_Schema/BuildSchema/BuildXMLSchema.aspx 这个网站在线转成xsd
<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="status">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="sigar">
<xsd:complexType>
<xsd:attribute name="cpu" type="xsd:decimal" />
<xsd:attribute name="memory" type="xsd:int" />
<xsd:attribute name="jvm" type="xsd:int" />
<xsd:attribute name="disk" type="xsd:int" />
</xsd:complexType>
</xsd:element>
<xsd:element name="jvm">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="memory">
<xsd:complexType>
<xsd:attribute name="free" type="xsd:int" />
<xsd:attribute name="total" type="xsd:int" />
<xsd:attribute name="max" type="xsd:int" />
</xsd:complexType>
</xsd:element>
<xsd:element maxOccurs="unbounded" name="memorypool">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="usageInit" type="xsd:int" />
<xsd:attribute name="usageCommitted" type="xsd:int" />
<xsd:attribute name="usageMax" type="xsd:int" />
<xsd:attribute name="usageUsed" type="xsd:int" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element maxOccurs="unbounded" name="connector">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="threadInfo">
<xsd:complexType>
<xsd:attribute name="maxThreads" type="xsd:int" />
<xsd:attribute name="currentThreadCount" type="xsd:int" />
<xsd:attribute name="currentThreadsBusy" type="xsd:int" />
</xsd:complexType>
</xsd:element>
<xsd:element name="requestInfo">
<xsd:complexType>
<xsd:attribute name="maxTime" type="xsd:int" />
<xsd:attribute name="processingTime" type="xsd:int" />
<xsd:attribute name="requestCount" type="xsd:int" />
<xsd:attribute name="errorCount" type="xsd:int" />
<xsd:attribute name="bytesReceived" type="xsd:int" />
<xsd:attribute name="bytesSent" type="xsd:int" />
</xsd:complexType>
</xsd:element>
<xsd:element name="workers">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="worker">
<xsd:complexType>
<xsd:attribute name="stage" type="xsd:string" />
<xsd:attribute name="requestProcessingTime" type="xsd:int" />
<xsd:attribute name="requestBytesSent" type="xsd:int" />
<xsd:attribute name="requestBytesReceived" type="xsd:int" />
<xsd:attribute name="remoteAddr" type="xsd:string" />
<xsd:attribute name="virtualHost" type="xsd:string" />
<xsd:attribute name="method" type="xsd:string" />
<xsd:attribute name="currentUri" type="xsd:string" />
<xsd:attribute name="currentQueryString" type="xsd:string" />
<xsd:attribute name="protocol" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
新增文件保存为 test.xsd 编码utf-16 改成utf-8
cmd 进入到文件下执行:
xjc test.xsd -p test.java
然后就可以看到生成的文件了, 就是这么的简单;
第二种方法
Jaxb
JAXB官网:http://jaxb.java.net/
API链接 :http://jaxb.java.net/nonav/2.2.5/docs/api/
文档 :http://jaxb.java.net/tutorial/
1. 利用trang.jar将Xml 转换成xsd
Trang 是一种命令行的可以XML生成XSD的工具,生成XML文件对应的XSD文件只需要3个简单的步骤:
第一步 下载Trang
从http://www.thaiopensource.com/relaxng/trang.html 上下载trang-version.zip
利用trang 生成xsd后, 之后的 动作跟上面一样;