0

I want to generate xml string in java programmatically whose name space is custom as shown below and all data must come dynamically in xml.How can I achieve something like this?

<faxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<person>
  <name>ABC</name>
</person>
</faxml>

I have gone through examples like this https://howtodoinjava.com/jaxb/write-object-to-xml/ but here when xml generated its starting line is

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

but I want start and end tag <faxml> and namespaces as I mentioned in my sample code as output

5
  • Are you getting the XML input? or as a string? Commented Aug 26, 2020 at 18:24
  • I want to generate xml from some data fetching from database like person name will come from database but starting of xml should be like "<faxml xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">" Commented Aug 26, 2020 at 18:26
  • I have typically seen this handled with JAXB annotations on fields - example, or by using package-info. Commented Aug 26, 2020 at 18:52
  • @andrewjames It was somewhat helpful but not completely solve my problem I don't require xml as root tag I require <faxml> as my root tag and xmlns:xsi etc as per posted Commented Aug 26, 2020 at 19:16
  • Understood - sorry, my mistake for not reading your question carefully. Strictly speaking the opening line <?xml...> is the XML declaration (also called the prolog, I think?) - which I believe is optional. It sounds like you want an XML file that has no declaration, and an opening tag of <faxml...> You have full control over the tags generated by JAXB; so maybe the question is: How do I suppress the declaration? (and maybe also: what are the consequences of doing so...?) Commented Aug 26, 2020 at 20:06

2 Answers 2

1

Here is one approach:

First, here is my class representing the required XML data:

package org.ajames.jaxb.persons;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "faxml")
public class Faxml {
    
    private Person person;
    
    public static class Person {
        
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
        
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
}

I chose to nest the Person class inside the root class - just for this test. There are other ways to arrange these classes, of course, so they are not nested.

Then I define the package-info for org.ajames.jaxb.persons as follows:

@XmlSchema(
        namespace = "",
        elementFormDefault = XmlNsForm.UNSET,
        xmlns = {
            @XmlNs(prefix = "", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
        })
package org.ajames.jaxb.persons;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;

To process the data from a Java object to XML, I use the following test data:

Faxml.Person person = new Faxml.Person();
person.setName("ABC");
Faxml faxml = new Faxml();
faxml.setPerson(person);

The JAXB context and marshaller are as follows, assuming we are writing the XML to a Java String, for this test:

JAXBContext jaxbContext = JAXBContext.newInstance(Faxml.class); 
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "test.xsd");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        
StringWriter stringWriter = new StringWriter();
marshaller.marshal(faxml, stringWriter);
String xml = stringWriter.toString();
        
System.out.println(xml);

The resulting XML is:

<faxml xsi:noNamespaceSchemaLocation="test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <person>
        <name>ABC</name>
    </person>
</faxml>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @andrewjames you saved lot of time!!
0
    String name= "name"     
    String createXml="<faxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"//
                      xsi:noNamespaceSchemaLocation="test.xsd">"//
                      +"<person>"//
                      +"<name>"+name+"</name>"//
                      +"</>person">"//
                      +"</faxml>";
                    Sysout(createXml);

get the name in a variable. Hard code these lines and insert it like this..

3 Comments

Yes this is possible but I will have bunch of data not just a single field I have only provided sample code but I want to create multiple bean classes and the value set to those bean classes needs to be converted to xml with root as <faxml ... String concat is not a feasible way for doing this
You can create a single class for this and the method can be initialised. So that you can call this method from all class. Dou you mean you will be getting "name", "age".. like that?
Yes I updated my question I want to achieve like the link I have provided but just need to replace <xml> tag and name space in output

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.