使用Java提取XML结构

时间:2017-04-04 13:53:32

标签: java xml structure

是否存在允许您提取/创建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文档的结构?

如果此过程被其他方式调用,请告诉我。

2 个答案:

答案 0 :(得分:0)

要获得有用的答案,您应该解释您希望能够对结果做些什么。

您可以使用http://www.freeformatter.com/xsd-generator.htmlhttp://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>

答案 1 :(得分:0)

下面的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>