2

我一直在尝试从类型Customer类对象的列表中生成 XML 文件。我已经成功完成了。但是我的 XML 文件的结构是嵌套的,我想将其展平。

以下是我的 XML 文件内容:

<Customers>
<customer>
    <accessRole>
        <name>Customer Center</name>
        <internalId>14</internalId>
    </accessRole>
    <aging>0.0</aging>
    <category>
        <name>Individual</name>
        <internalId>2</internalId>
    </category>
    <companyName>Wolfe Electronics</companyName>
    <consolAging>0.0</consolAging>
    <consolBalance>0.0</consolBalance>
    <consolDepositBalance>0.0</consolDepositBalance>
    <consolOverdueBalance>0.0</consolOverdueBalance>
    <consolUnbilledOrders>2705.23</consolUnbilledOrders>
    <creditHoldOverride/>
    <dateCreated>2011-06-22T12:30:00+05:30</dateCreated>
    <defaultAddress>US</defaultAddress>
    <displaySymbol>$</displaySymbol>
    <email>suresh@ldbsystems.com</email>
    <emailPreference/>
    <emailTransactions>false</emailTransactions>
    <entityId>A Wolfe</entityId>
    <entityStatus>
        <name>CUSTOMER-Closed Won</name>
        <internalId>13</internalId>
    </entityStatus>
    <externalId>entity-5</externalId>
    <faxTransactions>false</faxTransactions>
    <firstName>A</firstName>
    <firstVisit>2012-02-25T05:43:18+05:30</firstVisit>
    <giveAccess>true</giveAccess>
    <globalSubscriptionStatus/>
    <internalId>-5</internalId>
    <isBudgetApproved>false</isBudgetApproved>
    <isInactive>false</isInactive>
    <isPerson>true</isPerson>
    <lastModifiedDate>2012-12-19T07:12:03+05:30</lastModifiedDate>
    <lastName>Wolfe</lastName>
    <lastPageVisited>login-register</lastPageVisited>
    <lastVisit>2013-02-23T06:10:44+05:30</lastVisit>
    <overrideCurrencyFormat>false</overrideCurrencyFormat>
    <phone>650-555-9788</phone>
    <priceLevel>
        <name>Employee Price</name>
        <internalId>3</internalId>
    </priceLevel>
    <printTransactions>false</printTransactions>
    <receivablesAccount>
        <name>Use System Preference</name>
        <internalId>-10</internalId>
    </receivablesAccount>
    <salesRep>
        <name>Clark Koozer</name>
        <internalId>23</internalId>
    </salesRep>
    <shipComplete>false</shipComplete>
    <stage/>
    <symbolPlacement/>
    <taxItem>
        <name>CA-SAN MATEO</name>
        <internalId>-112</internalId>
    </taxItem>
    <taxable>false</taxable>
    <unbilledOrders>2705.23</unbilledOrders>
    <visits>150</visits>
    <webLead>No</webLead>
</customer>
<customer>
    <accessRole>
        <name>Customer Center</name>
        <internalId>14</internalId>
    </accessRole>
    <aging>0.0</aging>
    <category>
        <name>Corporate</name>
        <internalId>1</internalId>
    </category>
    <consolAging>0.0</consolAging>
    <consolBalance>0.0</consolBalance>
    <consolDepositBalance>0.0</consolDepositBalance>
    <consolOverdueBalance>0.0</consolOverdueBalance>
    <consolUnbilledOrders>76.8</consolUnbilledOrders>
    <creditHoldOverride/>
    <customFieldList>
        <customField/>
    </customFieldList>
    <dateCreated>2011-06-26T12:30:00+05:30</dateCreated>
    <defaultAddress>Anderson Boughton Inc.&lt;br&gt;1488 Main&lt;br&gt;Apt 113&lt;br&gt;Seattle WA 98106&lt;br&gt;US</defaultAddress>
    <displaySymbol>$</displaySymbol>
    <email>boughton751@cscatering.com</email>
    <emailPreference/>
    <emailTransactions>false</emailTransactions>
    <entityId>Anderson Boughton Inc.</entityId>
    <entityStatus>
        <name>CUSTOMER-Closed Won</name>
        <internalId>13</internalId>
    </entityStatus>
    <faxTransactions>false</faxTransactions>
    <firstVisit>2012-07-12T01:30:49+05:30</firstVisit>
    <giveAccess>false</giveAccess>
    <globalSubscriptionStatus/>
    <internalId>75</internalId>
    <isBudgetApproved>false</isBudgetApproved>
    <isInactive>false</isInactive>
    <isPerson>false</isPerson>
    <lastModifiedDate>2012-12-19T11:50:14+05:30</lastModifiedDate>
    <lastPageVisited>HP xw4100</lastPageVisited>
    <lastVisit>2012-07-12T01:30:49+05:30</lastVisit>
    <leadSource>
        <name>Partner Referral</name>
        <internalId>99993</internalId>
    </leadSource>
    <overrideCurrencyFormat>false</overrideCurrencyFormat>
    <partner>
        <name>Online electronics</name>
        <internalId>171</internalId>
    </partner>
    <phone>206-555-1302</phone>
    <priceLevel>
        <name>Base Price</name>
        <internalId>1</internalId>
    </priceLevel>
    <printTransactions>false</printTransactions>
    <receivablesAccount>
        <name>Use System Preference</name>
        <internalId>-10</internalId>
    </receivablesAccount>
    <salesRep>
        <name>A Wolfe</name>
        <internalId>-5</internalId>
    </salesRep>
    <shipComplete>false</shipComplete>
    <stage/>
    <symbolPlacement/>
    <taxable>false</taxable>
    <unbilledOrders>76.8</unbilledOrders>
    <webLead>No</webLead>
</customer>
</Customers>

所以我有嵌套结构<accessRole>,我想像这样展平 <accessRole-name>,我不想像<internalId>.

请找到我的模型的代码片段:

try {
             FileWriter fileWriter = new FileWriter ("D:\\adapter-framework\\Customer_XML.xml");
             JAXBContext jaxbContext = JAXBContext.newInstance(extractedRecordsArray[0].getClass());
             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
             fileWriter.append("<Customers>");
             fileWriter.append('\n');
             for(int i = 0; i < extractedRecordsArray.length - 761 ; i++)
             {          
                jaxbMarshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
                jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                //jaxbMarshaller.marshal(extractedRecordsArray[i],fileWriter);
                jaxbMarshaller.marshal(extractedRecordsArray[i],System.out);
                fileWriter.append('\n');
                AdapterLogger.debug(this.getClass().getName(), "Extracted record number : " + i);
             }        
            fileWriter.append("</Customers>");
            fileWriter.close();
          } catch (JAXBException e) {
            e.printStackTrace();
           }

如果有人知道,请帮助我。

谢谢。

4

1 回答 1

1

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

您可以使用@XmlPathMOXy 中的扩展来扁平化 XML 结构。

顾客

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlPath(".")
    private AccessRole accessRole;

}

访问角色

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class AccessRole {

    @XmlElement(name="accessRoleName")
    private String name;

}

了解更多信息

于 2013-06-18T13:06:41.197 回答