user9847788
user9847788

Reputation:

How to Programatically Update an XML Body and print the updated body

Below is my input XML body, which is stored in src/test/resources:

<Customer>
    <Id>1</Id>
    <Name>John</Name>
    <Age>23</Age>
</Customer>

I want to pass this XML body into the below java method, re-set the name & then post it to a HTTP URL:

public void updateXMLBodyAndPost(String newName(){
    File file = new File("src\\test\resources\\customerDetails.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Customer customer1 = (Customer) jaxbUnmarshaller.unmarshal(file);
    customer1.setName(newName);
    System.out.println("Customer details: " + customer1.toString());
}

Above, i am able to set the new name. Then, I am trying to print out the updated XML body

But, instead what's being printed out is:

Customer details: Customer@7ba4f24f

What changes do I need to make to my code so the following is printed out in the console:

<Customer>
    <Id>1</Id>
    <Name>updatedName</Name>
    <Age>23<Age>
</Customer>

Upvotes: 0

Views: 51

Answers (1)

Andrei Sfat
Andrei Sfat

Reputation: 8606

Customer details: Customer@7ba4f24f

You get this because you are calling .toString() and you have not included an implementation for it.

By default, every object, in your case Customer, has a default toString() which will result in a similar response as you have. You can read about the default toString() here.

Overriding toString:

public class Customer {

    private String name;
    private String id;
    private String age;

    @Override
    public String toString() {
        return "Customer{" +
                "name='" + name + '\'' +
                ", id='" + id + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

You can go in modifying the default toString() and make it return an xml, but I don't think that is a good idea, as toString() is not meant for that.

I would rather create a separate method where you unmarshal your xml from the file and marshal it back with the modifying customer data.

You can do the marshaling as:

    File file = Paths.get("src/main/resources/customer.xml").toFile();
    JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Customer customer1 = (Customer) jaxbUnmarshaller.unmarshal(file);
    customer1.setName(newName);

    // overriding toString
    System.out.println("Customer details with toString: " + customer1.toString());

    // print it nicely using JAXB
    final Marshaller marshaller = jaxbContext.createMarshaller();
    StringWriter sw = new StringWriter();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(customer1, sw);
    String xmlString = sw.toString();
    System.out.println("Customer details with JAXB.marshal: \n" + xmlString);

which would print:

Customer details with toString: Customer{name='dsadadada', id='1', age='23'}
Customer details with JAXB.marshal: 
<Customer>
    <Name>dsadadada</Name>
    <Id>1</Id>
    <Age>23</Age>
</Customer>

Upvotes: 2

Related Questions