Click here to Skip to main content
15,608,053 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to create the below XML document .

xml version="1.0" encoding="UTF-8"?>
<BCPFORMAT>
  <RECORD>
    <FIELD ID="1" xsi:type="CharFixed" MAX_LENGTH="4" />
  </RECORD>
</BCPFORMAT>


What I have tried:

I'm using the Java Code as below -

package com.tutorialspoint.xml;

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

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class createXmlLayout {
    public static void main(String[] args) {
        Document doc = new Document();
        Element root = new Element("BCPFORMAT");
        //RECORD Element
        Element child = new Element("RECORD");
        //FIELD Element
        Element name = new Element("FIELD")
                .setAttribute("ID", "1")
                .setAttribute("xsi:type", "CharFixed")
                .setAttribute("MAX_LENGTH", "4");

        child.addContent(name);
        root.addContent(child);
        doc.addContent(root);

        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        try {
            outputter.output(doc, System.out);
            outputter.output(doc, new FileWriter("c:\\VTG_MAPN.xml"));

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


I'm getting the below error -

The name "xsi:type" is not legal for JDOM/XML attributes: XML name 'xsi:type' cannot contain the character ":".

I know I might need to use Namespace but how I'm not able to figure out.

Please help!
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900