简体   繁体   English

如何从使用JAXB生成的XML文件中删除某些元素?

[英]How to remove some elements from XML file generated using JAXB?

I have been trying to generate XML file from lists of object of type Customer class. 我一直试图从类型为Customer类的对象列表中生成XML文件。 Which i have done successfully.But the structure of my XML file is nested and i want to flatten it out. 我已经成功完成了。但是我的XML文件的结构是嵌套的,我想把它弄平。

Following is my XML file content: 以下是我的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>

So i have nested structure for <accessRole> which i want to flatten out like <accessRole-name> and i don't want to display element like <internalId> . 所以我有<accessRole>嵌套结构,我想像<accessRole-name>那样扁平化,并且我不想显示像<internalId>那样的元素。

Please find a code snippet of my model: 请找到我模型的代码片段:

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();
           }

Please help me out if someone knows about it. 如果有人知道,请帮助我。

Thanks. 谢谢。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,并且是JAXB(JSR-222)专家组的成员。

You can use the @XmlPath extension in MOXy to flatten the XML structure. 您可以在@XmlPath中使用@XmlPath扩展来展平XML结构。

Customer 顾客

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

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

    @XmlPath(".")
    private AccessRole accessRole;

}

AccessRole AccessRole

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class AccessRole {

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

}

For More Information 欲获得更多信息

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

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