使用Java提取XML结构
问题描述:
是否有Java库可以让您提取/创建XML文档,因此最终结果是文档的“准系统”XML结构。使用Java提取XML结构
例
<result>
<rowset name="kills" key="killID" columns="killID,solarSystemID,killTime,moonID">
<row killID="54933226" solarSystemID="31001222" killTime="2016-07-03 22:29:19" moonID="0">
<victim characterID="93811169" characterName="Madcat326" corporationID="98008818" corporationName="Haight Industries LLC" allianceID="0" allianceName="" factionID="0" factionName="" damageTaken="63039" shipTypeID="17918" x="59788513854.179" y="-13687850177.3741" z="157817245029.115" />
<rowset name="attackers" columns="characterID,characterName,corporationID,corporationName,allianceID,allianceName,factionID,factionName,securityStatus,damageDone,finalBlow,weaponTypeID,shipTypeID">
<row characterID="91316135" characterName="Celeo Servasse" corporationID="98134538" corporationName="Wormbro" allianceID="0" allianceName="" factionID="0" factionName="" securityStatus="-1.0646369409683" damageDone="7825" finalBlow="1" weaponTypeID="3520" shipTypeID="12003" />
<row characterID="95273329" characterName="Mupoc Kashuken" corporationID="98134538" corporationName="Wormbro" allianceID="0" allianceName="" factionID="0" factionName="" securityStatus="-0.2" damageDone="16656" finalBlow="0" weaponTypeID="31882" shipTypeID="12023" />
</rowset>
<rowset name="items" columns="typeID,flag,qtyDropped,qtyDestroyed,singleton">
<row typeID="394" flag="20" qtyDropped="1" qtyDestroyed="0" singleton="0" />
<row typeID="24427" flag="32" qtyDropped="0" qtyDestroyed="1" singleton="0" />
<row typeID="2446" flag="87" qtyDropped="1" qtyDestroyed="1" singleton="0" />
<row typeID="28209" flag="87" qtyDropped="2" qtyDestroyed="0" singleton="0" />
</rowset>
</row>
</rowset>
</result>
将重建为类似
<result>
<rowset name="kills" key="killID" columns="killID,solarSystemID,killTime,moonID">
<row killID="54933226" solarSystemID="31001222" killTime="2016-07-03 22:29:19" moonID="0">
<victim characterID="93811169" characterName="Madcat326" corporationID="98008818" corporationName="Haight Industries LLC" allianceID="0" allianceName="" factionID="0" factionName="" damageTaken="63039" shipTypeID="17918" x="59788513854.179" y="-13687850177.3741" z="157817245029.115" />
<rowset name="attackers" columns="characterID,characterName,corporationID,corporationName,allianceID,allianceName,factionID,factionName,securityStatus,damageDone,finalBlow,weaponTypeID,shipTypeID">
<row characterID="91316135" characterName="Celeo Servasse" corporationID="98134538" corporationName="Wormbro" allianceID="0" allianceName="" factionID="0" factionName="" securityStatus="-1.0646369409683" damageDone="7825" finalBlow="1" weaponTypeID="3520" shipTypeID="12003" />
</rowset>
<rowset name="items" columns="typeID,flag,qtyDropped,qtyDestroyed,singleton">
<row typeID="394" flag="20" qtyDropped="1" qtyDestroyed="0" singleton="0" />
</rowset>
</row>
</rowset>
</result>
或者我怎么可以提取/创建XML文档的结构?
如果这个过程被称为别的东西,请让我知道。
答
要得到一个有用的答案,你应该解释你期望能够对结果做什么。
您可以使用在线工具(如http://www.freeformatter.com/xsd-generator.html或http://xmlgrid.net/xml2xsd.html)以XSD格式提取结构,那么您在寻找什么?
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="result">
<xs:complexType>
<xs:sequence>
<xs:element name="rowset">
<xs:complexType>
<xs:sequence>
<xs:element name="row">
<xs:complexType>
<xs:sequence>
<xs:element name="victim">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:int" name="characterID"/>
<xs:attribute type="xs:string" name="characterName"/>
<xs:attribute type="xs:int" name="corporationID"/>
<xs:attribute type="xs:string" name="corporationName"/>
<xs:attribute type="xs:byte" name="allianceID"/>
<xs:attribute type="xs:string" name="allianceName"/>
<xs:attribute type="xs:byte" name="factionID"/>
<xs:attribute type="xs:string" name="factionName"/>
<xs:attribute type="xs:int" name="damageTaken"/>
<xs:attribute type="xs:short" name="shipTypeID"/>
<xs:attribute type="xs:float" name="x"/>
<xs:attribute type="xs:float" name="y"/>
<xs:attribute type="xs:float" name="z"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="rowset" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="row" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:int" name="characterID" use="optional"/>
<xs:attribute type="xs:string" name="characterName" use="optional"/>
<xs:attribute type="xs:int" name="corporationID" use="optional"/>
<xs:attribute type="xs:string" name="corporationName" use="optional"/>
<xs:attribute type="xs:byte" name="allianceID" use="optional"/>
<xs:attribute type="xs:string" name="allianceName" use="optional"/>
<xs:attribute type="xs:byte" name="factionID" use="optional"/>
<xs:attribute type="xs:string" name="factionName" use="optional"/>
<xs:attribute type="xs:float" name="securityStatus" use="optional"/>
<xs:attribute type="xs:short" name="damageDone" use="optional"/>
<xs:attribute type="xs:byte" name="finalBlow" use="optional"/>
<xs:attribute type="xs:short" name="weaponTypeID" use="optional"/>
<xs:attribute type="xs:short" name="shipTypeID" use="optional"/>
<xs:attribute type="xs:short" name="typeID" use="optional"/>
<xs:attribute type="xs:byte" name="flag" use="optional"/>
<xs:attribute type="xs:byte" name="qtyDropped" use="optional"/>
<xs:attribute type="xs:byte" name="qtyDestroyed" use="optional"/>
<xs:attribute type="xs:byte" name="singleton" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
<xs:attribute type="xs:string" name="columns" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:int" name="killID"/>
<xs:attribute type="xs:int" name="solarSystemID"/>
<xs:attribute type="xs:string" name="killTime"/>
<xs:attribute type="xs:byte" name="moonID"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name"/>
<xs:attribute type="xs:string" name="key"/>
<xs:attribute type="xs:string" name="columns"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
答
以下XSLT转换格式可能有助于删除重复的子行元素。你可以从java代码运行这个转换器。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/result">
<result>
<rowset>
<xsl:copy-of select="rowset/@*"/>
<row>
<xsl:copy-of select="rowset/row/@*"/>
<victim>
<xsl:copy-of select="rowset/row/victim/@*"/>
</victim>
<rowset>
<xsl:copy-of select="rowset/row/rowset[@name='attackers']/@*"/>
<row>
<xsl:copy-of select="rowset/row/rowset[@name='attackers']/row[1]/@*"/>
</row>
</rowset>
<rowset>
<xsl:copy-of select="rowset/row/rowset[@name='items']/@*"/>
<row>
<xsl:copy-of select="rowset/row/rowset[@name='items']/row[1]/@*"/>
</row>
</rowset>
</row>
</rowset>
</result>
</xsl:template>
</xsl:stylesheet>
您的意思是你想要生成一个模式? – Paddyd
这听起来并不完全错误,我真正想要的是能够获取XML文档并获得其构建结构。 –