logo
down
shadow

I want to create this type of xml file using jaxb in eclipse


I want to create this type of xml file using jaxb in eclipse

Content Index :

I want to create this type of xml file using jaxb in eclipse
Tag : xml , By : tlync
Date : November 22 2020, 11:02 AM

With these it helps I Think you can do this way ..it is simple compared to above
ActionRoot.java
package com.naren;

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 = {
    "action"
})
@XmlRootElement(name = "action-root")
public class ActionRoot {

    @XmlElement(required = true)
    private Action action;

    /**
     * Gets the value of the action property.
     * 
     * @return
     *     possible object is
     *     {@link ActionRoot.Action }
     *     
     */
    public Action getAction() {
        return action;
    }

    /**
     * Sets the value of the action property.
     * 
     * @param value
     *     allowed object is
     *     {@link ActionRoot.Action }
     *     
     */
    public void setAction(Action value) {
        this.action = value;
    }

}
package com.naren;

import java.util.List;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "reportName", "reportVersion",
        "repNoOfParam", "repParams" })
public class Action {

    @XmlElement(name = "report-name", required = true)
    private String reportName;
    @XmlElement(name = "report-version")
    private int reportVersion;
    @XmlElement(name = "rep-no-of-param")
    private int repNoOfParam;
    @XmlElement(name = "rep-params")
    private List<RepParams> repParams;
    @XmlAttribute
    private String name;

    public String getReportName() {
        return reportName;
    }

    public void setReportName(String reportName) {
        this.reportName = reportName;
    }

    public int getReportVersion() {
        return reportVersion;
    }

    public void setReportVersion(int reportVersion) {
        this.reportVersion = reportVersion;
    }

    public int getRepNoOfParam() {
        return repNoOfParam;
    }

    public void setRepNoOfParam(int repNoOfParam) {
        this.repNoOfParam = repNoOfParam;
    }

    public List<RepParams> getRepParams() {
        return repParams;
    }

    public void setRepParams(List<RepParams> repParams) {
        this.repParams = repParams;
    }

    public String getName() {
        return name;
    }

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

}
package com.naren;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "value" })
public class RepParams {
    @XmlAttribute
    private String name;

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @XmlAttribute
    private int id;
    @XmlValue
    private String value;

}
package com.naren;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class TestXMLGen {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ActionRoot actionRoot = new ActionRoot();
        Action action = new Action();
        action.setName("EMPLOYERS");
        action.setRepNoOfParam((byte) 5);
        action.setReportVersion((byte) 12);
        action.setReportName("EMPLOYERS");
        List<RepParams> list = new ArrayList<RepParams>();

        RepParams repParams = new RepParams();
        repParams.setId((byte) 0);
        repParams.setName("P_LOCATION_ID");
        repParams.setValue("oadd_location_id");

        RepParams repParams2 = new RepParams();
        repParams2.setId((byte) 1);
        repParams2.setName("P_FINYEAR_ID");
        repParams2.setValue("oadd_fin_year_id");
        list.add(repParams);
        list.add(repParams2);
        action.setRepParams(list);
        actionRoot.setAction(action);
        try {

            File file = new File("file.xml");

            JAXBContext jaxbContext1 = JAXBContext
                    .newInstance(ActionRoot.class);
            Marshaller jaxbMarshaller = jaxbContext1.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(actionRoot, file);
            jaxbMarshaller.marshal(actionRoot, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

}
 package com.naren;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class TestUnMarshalXML {
    public static void main(String[] args) {

         try {

            File file = new File("file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(ActionRoot.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            ActionRoot aroot = (ActionRoot) jaxbUnmarshaller.unmarshal(file);

            Action action = aroot.getAction();
            List<RepParams> repParams=action.getRepParams();
            System.out.println(action.getName()+"\t"+action.getReportName()+"\t"+action.getReportVersion());
            for(RepParams rp:repParams)
                System.out.println(rp.getId()+"\t"+rp.getName()+"\t"+rp.getValue());

          } catch (JAXBException e) {
            e.printStackTrace();
          }

        }
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

JAXB eclipse error: There's no JAXB 2.2 API in the classpath


Tag : java , By : user86493
Date : March 29 2020, 07:55 AM
This might help you Make sure to include the jaxb-impl.jar library in your project. You can download one for example from here: http://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-impl/2.2.4/jaxb-impl-2.2.4.jar
If you're using Maven, you can include this as a dependency, otherwise download the file and add it as a library in your project.

Eclipse PDE headless build fails with: failed to create task or type eclipse.generateFeature


Tag : development , By : Sigfrieg
Date : March 29 2020, 07:55 AM
like below fixes the issue I found the solution in the discussion here: It is important to run the Ant script of the build within the same JRE as the workspace because otherwise the PDE Ant tasks cannot be found:
Right click on the Ant script (build.xml) > Run As > Ant Build … > JRE tab of the wizard > switch to option: 'Run in the same JRE as the workspace'

How to create nested Root Element in JAXB when creating xml from Java object using JAXB


Tag : java , By : Robby
Date : March 29 2020, 07:55 AM
it fixes the issue You didn't say which problem you have with your code. But I observed these things;
You need the @XmlRootElement only on your root element, i.e. on class Automobiles, but not on the others.
@XmlElement
public List<Bike> getBikes() { ... }
 @XmlElement(name = "bike")
 public List<Bike> getBikes() { ... }
 @XmlElement
 public List<Bike> getBike() { ... }

Validating xml against xsd file generated through JAXB and XSD build option in eclipse


Tag : java , By : nd27182
Date : March 29 2020, 07:55 AM
I hope this helps you . The generated code is on github for you.
I'm gussing that it's a problem with the xml or xsd files -- but I don't know for a fact.
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ ls
foo.xml
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ cat foo.xml 
<MLIspec> 
<id>4050response</id> 
<typename>4050Response</typename> 
<description>Show the Contract Numbers</description>    
<element> 
<id>7504</id> 
<name>SourceAccountIdentifier</name> 
<type>table</type> 
<required>true</required>       
</element>
</MLIspec>
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ trang foo.xml foo.xsd
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ cat foo.xsd 
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="MLIspec">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="id"/>
        <xs:element ref="typename"/>
        <xs:element ref="description"/>
        <xs:element ref="element"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="typename" type="xs:NMTOKEN"/>
  <xs:element name="description" type="xs:string"/>
  <xs:element name="element">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="id"/>
        <xs:element ref="name"/>
        <xs:element ref="type"/>
        <xs:element ref="required"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="name" type="xs:NCName"/>
  <xs:element name="type" type="xs:NCName"/>
  <xs:element name="required" type="xs:boolean"/>
  <xs:element name="id" type="xs:NMTOKEN"/>
</xs:schema>
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ xjc foo.xsd 
parsing a schema...
compiling a schema...
generated/Element.java
generated/MLIspec.java
generated/ObjectFactory.java
thufir@dur:~/xml/so$ 
thufir@dur:~/xml/so$ tree
.
├── foo.xml
├── foo.xsd
└── generated
    ├── Element.java
    ├── MLIspec.java
    └── ObjectFactory.java

1 directory, 5 files
thufir@dur:~/xml/so$ 
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ cat /home/thufir/xml/output.xml 
cat: /home/thufir/xml/output.xml: No such file or directory
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ git clone git@github.com:THUFIR/xjc_generated-code.git
Cloning into 'xjc_generated-code'...
remote: Enumerating objects: 143, done.
remote: Counting objects: 100% (143/143), done.
remote: Compressing objects: 100% (81/81), done.
remote: Total 143 (delta 25), reused 120 (delta 11), pack-reused 0
Receiving objects: 100% (143/143), 79.43 KiB | 191.00 KiB/s, done.
Resolving deltas: 100% (25/25), done.
thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ cd xjc_generated-code/
thufir@dur:~/NetBeansProjects/xjc_generated-code$ 
thufir@dur:~/NetBeansProjects/xjc_generated-code$ gradle clean run

> Task :run
Jan 17, 2019 2:34:06 PM xjc_generated.code.driver.App unmarshallMLI
INFO: generated.MLIspec@12edcd21

BUILD SUCCESSFUL in 1s
4 actionable tasks: 3 executed, 1 up-to-date
thufir@dur:~/NetBeansProjects/xjc_generated-code$ 
thufir@dur:~/NetBeansProjects/xjc_generated-code$ cat /home/thufir/xml/output.xml 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MLIspec>
    <id>4050response</id>
    <typename>4050Response</typename>
    <description>Show the Contract Numbers</description>
    <element>
        <id>7504</id>
        <name>SourceAccountIdentifier</name>
        <type>table</type>
        <required>true</required>
    </element>
</MLIspec>
thufir@dur:~/NetBeansProjects/xjc_generated-code$ 

cvc-complex-type.3.2.2: Attribute 'jaxb:extensionBindingPrefixes' is not allowed to appear in element 'jaxb:bindings'


Tag : eclipse , By : Terrence Poon
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How to migrate from XslTransform to XslCompiledTransform
  • Is there any better IDOMImplementation other than MSXML?
  • Indesign and XML - how to auto flow into multiple pages with differing styles?
  • XSLT - Add a XML node as header
  • Would like to validate a reference to a container in an XML
  • Dataweave - Trying to write END_DOCUMENT when document has no root
  • How can i export Orbeon form data into xml
  • Using XMLQUERY to retrieve specific XML data
  • Is there a semantical difference between <tag /> and <tag></tag> in XML?
  • Importing xml with nested nodes into Access (and needing to transform the xml into a 'flat' structure with xsl)
  • Why is my modified XML not saving the modified values?
  • How to ensure certain immediate siblings always appear in a specific order?
  • How do you handle complex namespaces in XQuery/BaseX?
  • Removing invalid characters from XML in Python3 with etree
  • Grouping/Paginating items from two different parents
  • contracted/expanded tabular display for all principal xml lines
  • How to establish a custom xml serialization with content negotiation in spring boot 2
  • Overcome 2GB limit on XML output in SQL Server
  • How to add a node to an existing XML document using powershell
  • Why is the attribute also append to the child node?
  • Escaping a Dash in XML Element Name With Powershell
  • What is the use of XML?
  • Create dynamic list/array of string - int pairs based on XML input
  • How to flatten a very complex XML into a new XML containing all nodes at root level
  • Looping and summing values from a different tag values in XSLT
  • XML to csv do the correct xslt
  • XML 'name' attribute, and XML variable type with SelectNodes
  • Remove elements from XML based on complex condition
  • Doctrine Invalid Mapping File Exception when trying to generate entities
  • Is it possible to use icons next to radio buttons in odoo 12
  • View responsible for top navbar in Odoo12
  • Best way to query different xml elements as one column
  • unexpected token while reading xml to string in JSR223 sampler
  • Concatenate XML in SQL stored Procedure
  • XSL - Only getting data from one node in my XML
  • XML schema - No matching global declaration available for the validation root
  • How to duplicate an entire XML Node and insert it in the XML data using TSQL
  • Groovy to get node values from XML in SoapUI response
  • XmlEntries to single CosmosDB entries with Azure Function
  • Create child in XML within node by attribute name
  • How to search and edit an xml tag that matches a condtion using xmlstarlet
  • Google App Script XmlService addNamespace method is adding blank XML namespaces to child elements
  • Convert CSV to XML using Mule 4
  • Why is xsl:when and xsl:value-of not working here?
  • Problem to generate HTML page via XML and XSL (i18n context)
  • How can I pass info about a referring page in a B2C claim
  • TSQL: How to get a UNION result from two tables with XML information in T1.Field_A and nvarchar in T2.Field_A
  • Make different changes in each line of bash script based on a list file
  • Xslt 1.0 if element value from a node is true then grab the value of another element from the same node
  • Is it possible to include one select in another?
  • Split XML file into multiple files using XSLT
  • simpletype is invalid in this context
  • XSLT select all elements except 3 and put them in a new field
  • Failed to parse a schema xsd
  • Converting local types to global types in XML Schema
  • Search and return tag name based off attribute within the tag xml bash
  • How to proper add xml namespace in an xmlstarlet update command when along used with XPath
  • XML Schema - Element attribute with different datatypes (need a simple typ definition)
  • Why is this xslt copy-of example not returning expected result
  • "not a schema document" when validating XML schema with notepad++ XML tools
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com