0

I cannot change the name of the tags in the XML that is returned by Spring MVC Controller, the Srpring returns XML tags as Java object names and not as expected. I have used @XmlElement for setting the tag names as required but does not work.

I have tried adding XmlElement at attributes instead of getters, didn't work, i have looked for a lot of solutions but none have worked, i have added all the necessary JAXB annotations for this to work, please help as i can't progress any further on this.

Controller:

@RequestMapping(value = "/customerAttributes/{number}", method = 
RequestMethod.GET, produces = "application/xml")
@ResponseBody
public Customer getCustInfo(@PathVariable String number) {

  Address add = new Address();
  Customer cust = new Customer();

  add.setHouse(number);
  add.setStreet("Street");
  cust.setName("Ankush");
  cust.setAdd(add);
  return cust;
  }

Customer:

package com.fti.di.customer.domain;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"Name","add"})
@XmlRootElement(name = "DI_Customer")
public class Customer {

private String name;
private Address add;

@XmlElement(name = "DI_Name", required = true)
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@XmlElement(name = "DI_Add", required = true)
public Address getAdd() {
    return add;
}
public void setAdd(Address add) {
    this.add = add;
}   
}

Address:

package com.fti.di.customer.domain;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "",propOrder = {"Street","House"})
public class Address {

private String street;
private String house;

@XmlElement(name = "DI_Street")
public String getStreet() {
    return street;
}
public void setStreet(String street) {
    this.street = street;
}
@XmlElement(name = "DI_House")
public String getHouse() {
    return house;
}
public void setHouse(String house) {
    this.house = house;
}   
}

Result:

<Customer>
  <add>
      <street>Street</street>
      <house>3012</house>
  </add>
  <name>Ankush</name>
</Customer>

Expected:

<DI_Customer>
  <DI_Add>
      <DI_Street>Street</DI_Street>
      <DI_House>3012</DI_House>
  </DI_Add>
  <DI_Name>Ankush</DI_Name>
</DI_Customer>

3 Answers 3

2

This solution works fine for me :

@XmlType(name = "",propOrder = {"street","house"})
@XmlRootElement(name = "DI_Add")
public static class Address {

    private String street;
    private String house;

    @XmlElement(name = "DI_Street")
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    @XmlElement(name = "DI_House")
    public String getHouse() {
        return house;
    }
    public void setHouse(String house) {
        this.house = house;
    }
}

@XmlType(name = "", propOrder = {"name","add"})
@XmlRootElement(name = "DI_Customer")
public static class Customer {

    private String name;
    private Address add;

    @XmlElement(name = "DI_Name", required = true)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlElement(name = "DI_Add", required = true)
    public Address getAdd() {
        return add;
    }
    public void setAdd(Address add) {
        this.add = add;
    }
}

Be careful to propOrderwhich is case sensitive.

Sign up to request clarification or add additional context in comments.

4 Comments

Why did you add static before class names? Also i copied your code and tried using it, i had jackson dependency in my POM files, i removed it as i read that jackson has precedence over JAXB parsers but it still does not work. Is there any other way?
My bad, I added static because I created those classes as inner class. I have jackson too in my classpath. Can you provide your dependencies?
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.1.6.RELEASE</version> <scope>${}</scope> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.5</version> <scope>${}</scope> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.5</version> <scope>${}</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> <scope></scope> </dependency>
For scope, it's provided.
0

The answer by Victor will solve your problem. What he did is that he removed the @XmlAccessorType(XmlAccessType.FIELD) annotation from class Customer.

You can also in class Customer:

  • Keep @XmlAccessorType(XmlAccessType.FIELD) and move the @XmlElement annotations above the fields (instead of the getters)
  • Keep your annotations where they are (i.e., getters) and change to @XmlAccessorType(XmlAccessType.PROPERTY)

The explanation for their difference and an example showcasing them in practice can be found here

2 Comments

Where should i add @XmlAccessorType(XmlAccessType.PROPERTY) ?
@AnkushTyagi in class Customer you have annotation @XmlAccessorType(XmlAccessType.FIELD). You can change it to @XmlAccessorType(XmlAccessType.PROPERTY)
0
from bs4 import BeautifulSoup
import requests
from contextlib import closing
from selenium.webdriver import Chrome # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
import selenium 
import os
import re
import csv

import sys

pPortalBS = requests.get("https://sapeadfs.sapient.com/adfs/ls?wa=wsignin1.0&wtrealm=urn%3asamlcolo%3apeopleportal&wctx=https%3a%2f%2fpeopleportal2.sapient.com%2fmy%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F")
pPortal = BeautifulSoup(pPortalBS.content,"html5lib")

#print pPortal

chromedriver = "C:/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
#driver = webdriver.Chrome(chromedriver)
#12,18,30,39,48,76,78
i=0
logged_in_flag = 0
with closing(Chrome(chromedriver)) as driver:
    while(1):
        try:
            driver.get("https://sapeadfs.sapient.com/adfs/ls?wa=wsignin1.0&wtrealm=urn%3asamlcolo%3apeopleportal&wctx=https%3a%2f%2fpeopleportal2.sapient.com%2fmy%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F")
            '''
            try:
                element = WebDriverWait(driver, 20).until(
                EC.presence_of_element_located((By.ID, "hrdSelection"))
                )
            finally:
                driver.quit()
            '''
            #button = driver.find_element_by_id('idp')
            page_source = driver.page_source
            #page_source
            button = driver.find_elements_by_xpath(".//*/div[@class='idpDescription float']")
            print button
            button[1].click()
            # wait for the page to load
            element = WebDriverWait(driver, 10).until(
            EC.invisibility_of_element_located((By.ID, "hrdSelection"))
            )
            # store it to string variable
            page_source = driver.page_source

            #logging in

            if(logged_in_flag ==0):
                try:
                    usernameInput = driver.find_element_by_name("UserName")
                    usernameInput.send_keys("[email protected]")
                    passwordInput = driver.find_element_by_id("passwordInput")
                    passwordInput.send_keys("Xendering@678")

                    driver.find_element_by_id("submitButton").click()
                    logged_in_flag = 1
                except:
                    logged_in_flag = 1



            #people portal home

            with closing(open("Go West Resource List.csv", "r")) as ifile:

                with closing(open("Go West Resource List Out Detailed Missing.csv", "a")) as ofile:

                    fileWriter = csv.writer(ofile)
                    reader = csv.reader(ifile)
                    rownum = 0
                    print reader

                    for row in reader:
                        # Save header row.
                        #print rownum
                        newRow = row
                        if rownum == 0:
                            header = row
                        elif rownum in range(1,i+1):
                            rownum = rownum +1
                            continue;
                        else:
                            colnum = 0
                            for col in row:
                                if(colnum == 1):
                                    if int(col) in [107590,21471,54226,130525,127212,125423,120988,48426,46183,59615,89696,88052,120465,59615,56277,119013,39460,46183,22920,79590,43848,25908,52556,60466,107439,100470,107439,105611,78666,78666,82833,59615,101430,59615,117872,82833,20332,117872,22920,23703,84858,59615,59615,63648,185,105611,121585,120043,107439,79564,22116,46183,91066,51892,46183,52591,77173,77762,91238,1018,129661,1018,113662,49410,24560,127848,46982,46183,133378,46982,57335,46982,46982,46982,52591,46982,77173,46982,46982,46982,46982,46982,46982,46982,46982,90166,50615,46183,46183,21702,115801,46183,23703,23703,219,49410,132,1127,339,46183,24560,1607,23703,665,78666,117872,43544,21235,1127,110479,99,219,46738,82002,182,23531,62022,128329,21668,62924,22738,46183,219,20923,54262,21702,118739,178,49220,23703,99,61075,660,77762,117872,21702,70423,28588,21795,101430,46183,83720,70927,33632,82334,41456,50892,25908,109358,117362,32474,90166,24560,31523,48632,90166,88052,49410,81322,38700,59615]:
                                    #1049
                                        print "ok"
                                    else:
                                        continue

                                    #try:
                                    print (header[colnum], col)
                                    #col = 133916
                                    element = WebDriverWait(driver, 20).until(
                                    EC.visibility_of_element_located((By.ID, "txtSearchPeople"))
                                    )
                                    searchbox = driver.find_element_by_id("txtSearchPeople")
                                    #oidInput = driver.find_element_by_id("passwordInput")
                                    searchbox.send_keys(col)
                                    searchbutton = driver.find_element_by_id("lbtnGoPeople")
                                    searchbutton.click()
                                    person = driver.find_element_by_id("NameFieldLink").click()
                                    element = WebDriverWait(driver, 10).until(
                                    EC.visibility_of_element_located((By.ID, "dvMyInformationContent"))
                                    )

                                    element = WebDriverWait(driver, 10).until(
                                    EC.visibility_of_element_located((By.ID, "dvAssignmentDetails"))
                                    )

                                    element = WebDriverWait(driver, 10).until(
                                    EC.visibility_of_element_located((By.ID, "tblPhoneNumberAddEdit"))
                                    )
                                    page_source = driver.page_source
                                    pPortal = BeautifulSoup(page_source, "html5lib")
                                    informationWindow = pPortal.find("div",{"id":"dvMyInformationContent"})
                                    #print informationWindow.text
                                    informationWindowRows = informationWindow.findAll("div",{"class":"row"})
                                    startDate = ""
                                    IM=""
                                    for informationWindowRow in informationWindowRows:
                                        informationWindowRowData = informationWindowRow.findAll("div")
                                    #print informationWindowRowData
                                        foundDateFlag = 0
                                        foundIMflag = 0
                                        foundEmailFlag = 0
                                        IM_visited = 0
                                        SD_visited = 0
                                        EM_visited = 0
                                        for data in informationWindowRowData:
                                            #print data.text
                                            if(data.text == "IM"):
                                                foundIMflag = 1
                                                continue;
                                            if(foundIMflag == 1 and IM_visited == 0):   
                                                IM = data.text
                                                newRow.append(IM)
                                                IM_visited = 1



                                            if(data.text == "Start Date"):
                                                foundDateFlag = 1
                                                continue;
                                            if(foundDateFlag == 1 and SD_visited == 0): 
                                                startDate = data.text
                                                newRow.append(startDate)
                                                SD_visited = 1

                                            if(data.text == "Email"):
                                                foundEmailFlag = 1
                                                continue;
                                            if(foundEmailFlag == 1 and EM_visited == 0):    
                                                email = data.text
                                                newRow.append(email)
                                                EM_visited = 1



                                        if(foundDateFlag ==1 and foundIMflag == 1 and foundEmailflag == 1):
                                            break;


                                    titleBS = pPortal.find("div",{"class":"myprofile-name"})    
                                    titleSpans = titleBS.findAll("span")
                                    title = titleSpans[-1].text
                                    newRow.append(title)

                                    home_location = ""
                                    current_location = ""
                                    employee_category = ""
                                    comp_approver = ""
                                    comp_communicator = ""
                                    people_manager = ""

                                    assignmentDetails = pPortal.find("div",{"id":"dvAssignmentDetails"})        
                                    detailsDivs = assignmentDetails.findAll("div",{"class":"table tblBorder"})
                                    for detailDiv in detailsDivs:
                                        rows = detailDiv.findAll("div",{"class":"row"})
                                        for row in rows:
                                            divs = row.findAll("div")

                                            if(divs[0].text == "Home Location"):
                                                home_location = divs[1].text
                                                print home_location
                                            if(divs[0].text == "Current Location"):
                                                current_location = divs[1].text

                                            if(divs[0].text == "Employee Category"):
                                                employee_category = divs[1].text

                                            if(divs[0].text == "Comp Approver"):

                                                comp_approver = divs[1].text
                                            if(divs[0].text == "Comp Communicator"):
                                                comp_communicator = divs[1].text

                                            if(divs[0].text == "People Manager"):
                                                people_manager = divs[1].text

                                    newRow.append(home_location)
                                    newRow.append(current_location)
                                    newRow.append(employee_category)
                                    newRow.append(people_manager)
                                    newRow.append(comp_approver)
                                    newRow.append(comp_communicator)

                                    phoneWindow = pPortal.find("table",{"id":"tblPhoneNumberAddEdit"})      
                                    details = phoneWindow.find("tbody",{})

                                    trs = details.findAll("tr")
                                    phoneNos = []
                                    for tr in trs:
                                        td = tr.findAll("td")
                                        phoneNos.append(str(td[0].text))

                                    newRow.append(phoneNos)
                                    fileWriter.writerow(newRow)
                                    i=i+1

                                    print "success "+str(i)
                                    #except:
                                    #   continue;
                                colnum = colnum + 1
                        rownum = rownum + 1

                    print "end"
            break;
        except selenium.common.exceptions.NoSuchElementException, e:
            print "fail "+str(i)
            print str(e)

            i=i+1
            continue
        except Exception, e:
            print "fail "+str(i)
            print str(e)
            i=i+1
            logged_in_flag = 1
            continue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.