我如何解组从我硬编码的XML创建DefaultValues?

时间:2017-01-19 04:50:01

标签: java xml xml-parsing jaxb unmarshalling

我在我的程序中对这些值进行了硬编码。我想通过XML文件阅读。

private void setDefaultValues() {
    Role syseng = new Role("System Engineer"); // Create a New Role -
                                                // Name - System Engineer,
                                                // variable name - syseng

    O_Roles.addOrganization(syseng); // Add to Organization (O_Roles)

    Associate aishwarya = new Associate("Aishwarya ",
            "aishwarya.nambiar@cerner.com", "954567554");
    Associate harsh = new Associate("Kumar Harsh",
            "kumar.harsh@cerner.com", "8555422835");
    Associate nikhil = new Associate("Nikhil kumar",
            "nikhil.kumar@cerner.com", "9538756673");

    syseng.addAssociate(aishwarya); // Add Associate Object to Role
                                    // named syseng ("System Engineer)
    syseng.addAssociate(harsh);
    syseng.addAssociate(nikhil);
    // /End of System Engineer Definition

    Role sweng = new Role("Software Engineer");
    O_Roles.addOrganization(sweng);

    Associate tansheet = new Associate("Tansheet Izhad",
            "tansheet.izhad@cerner.com", "938579467");
    Associate puja = new Associate("Puja Mishra", "puja.misra@cerner.com",
            "7406062967");
    Associate anand = new Associate("Anand Prakash",
            "anand.prakash@cerner.com", "9745745935");

    sweng.addAssociate(tansheet);
    sweng.addAssociate(puja);
    sweng.addAssociate(anand);
    // /End of Software Engineer Definition

    Role supeng = new Role("Support Engineer");
    O_Roles.addOrganization(supeng);

    supeng.addAssociate(new Associate("Sambhavi Pandey",
            "sambhavi.pandey@cerner.com", "9346572239"));
    supeng.addAssociate(new Associate("Sadique Raza",
            "sadique.raza@cerner.com", "946355445"));

    // /End of Support Engineer Definition
}

我试图将程序转换为XML。并且该方法看起来像这样,它返回从XML解组它实际需要的细节。 由于我的项目有3个模型类。

1- Associate
2-组
3-组织

我在解组时遇到了麻烦。下面的Marshalled XML文件看起来。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<organization>
    <role_list>
        <associates_list>
            <m_name>Aishwarya</m_name>
            <m_email>aishwarya.nambiar@cerner.com</m_email>
            <m_phone>954567554</m_phone>
        </associates_list>
        <associates_list>
            <m_name>Kumar Harsh</m_name>
            <m_email>kumar.harsh@cerner.com</m_email>
            <m_phone>8555422835</m_phone>
        </associates_list>
        <associates_list>
            <m_name>Nikhil kumar</m_name>
            <m_email>nikhil.kumar@cerner.com</m_email>
            <m_phone>9538756673</m_phone>
        </associates_list>
        <role_name>System Engineer</role_name>
    </role_list>
    <role_list>
        <associates_list>
            <m_name>Tansheet Izhad</m_name>
            <m_email>tansheet.izhad@cerner.com</m_email>
            <m_phone>938579467</m_phone>
        </associates_list>
        <associates_list>
            <m_name>Puja Mishra</m_name>
            <m_email>puja.misra@cerner.com</m_email>
            <m_phone>7406062967</m_phone>
        </associates_list>
        <associates_list>
            <m_name>Anand Prakash</m_name>
            <m_email>anand.prakash@cerner.com</m_email>
            <m_phone>9745745935</m_phone>
        </associates_list>
        <role_name>Software Engineer</role_name>
    </role_list>
    <role_list>
        <associates_list>
            <m_name>Sambhavi Pandey</m_name>
            <m_email>sambhavi.pandey@cerner.com</m_email>
            <m_phone>9346572239</m_phone>
        </associates_list>
        <associates_list>
            <m_name>Sadique Raza</m_name>
            <m_email>sadique.raza@cerner.com</m_email>
            <m_phone>946355445</m_phone>
        </associates_list>
        <role_name>Support Engineer</role_name>
    </role_list>
</organization>

使用以下方法。

private static void marshaling() throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(Organization.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // Marshal the O_Roles list in console
        jaxbMarshaller.marshal(O_Roles, System.out);

        // Marshal the O_Roles list in file
        jaxbMarshaller.marshal(O_Roles, new File(
                "C:/Users/Public/associate.xml"));
    }

0 个答案:

没有答案