简体   繁体   English

XML组信息(按行)

[英]XML group information by row

I have a specific database with this particular form : 我有一个特定格式的特定数据库:

<Database  NumTables="5">
    <Table TableName="table_name_0" TableID="0" NumOriginalColumns="8" NumColumns="8" NumRows="5">
        <Column ColumnName="column_name_0" ColumnID="0" ColumnType="Int32">
            <Cell>1</Cell>
            <Cell>2</Cell>
            <Cell>3</Cell>
            <Cell>4</Cell>
            <Cell>5</Cell>
        </Column>
        <Column ColumnName="column_name_1" ColumnID="1" ColumnType="Int16">
            <Cell>5</Cell>
            <Cell>2</Cell>
            <Cell>8</Cell>
            <Cell>32</Cell>
            <Cell>42</Cell>
        </Column>
    ... (other columns)
    </Table>
    ... (other tables)
</Database>

And when I want to group information by rows I do like this : 当我想按行对信息进行分组时,我会这样:

Element root = this.document.getRootElement();    
Element table = root.getChildren().get(0);
int numRows = Integer.parseInt(table.getAttributeValue("NumRows"));
for (int currentRow = 0; currentRow < numRows; currentRow++) {
    Integer column_0 = Integer.parseInt(table.getChildren().get(0).getChildren().get(currentRow).getValue());
    Integer column_1 = Integer.parseInt(table.getChildren().get(1).getChildren().get(currentRow).getValue());
    ...
}

It works well, but I think this code is not very nice because I use two times the construction getChildren.get(number) who is very cumbersome. 它运行良好,但是我认为这段代码不是很好,因为我使用了两次构造getChildren.get(number) ,这是非常麻烦的。 So I want, to know if it exist a proper solution to do this with jdom2. 因此,我想知道是否存在使用jdom2执行此操作的适当解决方案。

This is the first step: an XML schema describing your XML file, call it db.xsd 这是第一步:描述您的XML文件的XML模式,将其db.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        version="2.0">
<xs:element name="Database" type="Database"/>
<xs:complexType name="Database">
 <xs:sequence>
   <xs:element name="Table" type="Table" maxOccurs="unbounded"/>
 </xs:sequence>
 <xs:attribute name="NumTables"  type="xs:int"/>
</xs:complexType>
<xs:complexType name="Table">
 <xs:sequence>
   <xs:element name="Column" type="Column" maxOccurs="unbounded"/>
 </xs:sequence>
 <xs:attribute name="TableName"          type="xs:string"/>
 <xs:attribute name="TableID"            type="xs:int"/>
 <xs:attribute name="NumOriginalColumns" type="xs:int"/>
 <xs:attribute name="NumColumns"         type="xs:int"/>
 <xs:attribute name="NumRows"            type="xs:int"/>
</xs:complexType>
<xs:complexType name="Column">
 <xs:sequence>
   <xs:element name="Cell" type="xs:string" maxOccurs="unbounded"/>
 </xs:sequence>
 <xs:attribute name="ColumnName" type="xs:string"/>
 <xs:attribute name="ColumnID"   type="xs:int"/>
 <xs:attribute name="ColumnType" type="xs:string"/>
</xs:complexType>
</xs:schema>

If you run 如果你跑

$ xjc db.xsd

Several Java classes are generated in subdir generated (this name can, of course, be improved). 在生成的subdir中generated几个Java类(当然,可以改进此名称)。

Now, we need a simple procedure to unmarshal your XML file: 现在,我们需要一个简单的过程来解组XML文件:

public class Main {

private static final String PACKAGE = "generated";
private static final String SCHEMA  = "table.xsd";
private static final String XMLIN   = "database.xml";

private Database db;

void unmarshal() throws Exception {
    JAXBContext jc = JAXBContext.newInstance( PACKAGE );
    Unmarshaller m = jc.createUnmarshaller();

    JAXBElement<Database> jbe = null;
    try {
        StreamSource source =
            new StreamSource( new FileInputStream( XMLIN ) );
        jbe = m.unmarshal( source, Database.class );
        db = jbe.getValue();
    } catch( Exception e  ){
        System.out.println( "EXCEPTION: " + e.getMessage() );
        e.printStackTrace();
}
}

public Database getDatabase(){
    return db;
}

public static void main( String[] args ) {
    Main main = new Main();
    try {
        main.unmarshal();
        Database db = main.getDatabase();
        for( Table table: db.getTable() ){
            for( Column column: table.getColumn() ){
                 for( String cell: column.getCell() ){
                     System.out.print( " " + cell );
                }
            } 
        }       
    } catch( Exception e ){
        System.err.println( "marshal fails: " );
        e.printStackTrace();
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM