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

Jay Shah
  • 732
  • 4
  • 16
  • Are you getting the XML input? or as a string? – S14321K Aug 26 '20 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 "" – Jay Shah Aug 26 '20 at 18:26
  • I have typically seen this handled with JAXB annotations on fields - [example](https://stackoverflow.com/questions/46308489/jaxb-marshal-namespace-prefix), or by using [`package-info`](https://stackoverflow.com/questions/6294065/jaxb-namespace-prefixes-missing). – andrewJames Aug 26 '20 at 18:52
  • @andrewjames It was somewhat helpful but not completely solve my problem I don't require xml as root tag I require as my root tag and xmlns:xsi etc as per posted – Jay Shah Aug 26 '20 at 19:16
  • Understood - sorry, my mistake for not reading your question carefully. Strictly speaking the opening line `` 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 `` 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...?) – andrewJames Aug 26 '20 at 20:06

2 Answers2

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>
andrewJames
  • 19,570
  • 8
  • 19
  • 51
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..

S14321K
  • 220
  • 3
  • 19
  • 1
    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 – Jay Shah Aug 26 '20 at 18:33
  • 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? – S14321K Aug 26 '20 at 18:39
  • Yes I updated my question I want to achieve like the link I have provided but just need to replace tag and name space in output – Jay Shah Aug 26 '20 at 18:42