1

I am currently trying to create an XML document in JAVA using DOM parser. I'm using another answer that was posted in another question and it has helped me a lot however that answer doesn't go into enough detail to help me with a couple of the cases that I have. Let me begin by showing how I have been declaring my XML file thus far:

package creatingXML;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateXML 

{

    public static void main(String args[]){

        try{

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            Element rootElement = doc.createElement("Document");
            doc.appendChild(rootElement);

            Element BkToCstmrDbtCdtNtfctn = doc.createElement("BkToCstmrDbtCdNtfctn");
            rootElement.appendChild(BkToCstmrDbtCdtNtfctn);

            Element GrpHdr = doc.createElement("GrpHdr");
            BkToCstmrDbtCdtNtfctn.appendChild(GrpHdr);

            Element MsgId = doc.createElement("MsgId");
            GrpHdr.appendChild(MsgId);
            MsgId.appendChild(doc.createTextNode("0000000")); //MSG ID WILL GO HERE

            Element CreDtTm = doc.createElement("CreDtTm");
            GrpHdr.appendChild(CreDtTm);
            CreDtTm.appendChild(doc.createTextNode("2016-03-31T02:51:44"));     //CREDIT DATE TIME WILL GO HERE

            //MsgRcpt Tree
            Element MsgRcpt = doc.createElement("MsgRcpt");
            GrpHdr.appendChild(MsgRcpt);
            Element Id = doc.createElement("Id");
            MsgRcpt.appendChild(Id);
            Element OrgId = doc.createElement("OrgId");
            Id.appendChild(OrgId);
            Element Othr = doc.createElement("Othr");
            OrgId.appendChild(Othr);
            Element Id2 = doc.createElement("Id");
            Othr.appendChild(Id2);
            Id2.appendChild(doc.createTextNode("CS"));      //Org ID will go here!


            Element Ntfctn = doc.createElement("Ntfctn");
            BkToCstmrDbtCdtNtfctn.appendChild(Ntfctn);

            Element Id3 = doc.createElement("Id");
            Ntfctn.appendChild(Id3);
            Id3.appendChild(doc.createTextNode("163V2514435W14QI"));    //Transaction ID will go here!

            Element CreDtTm2 = doc.createElement("CreDtTm");
            Ntfctn.appendChild(CreDtTm2);
            CreDtTm2.appendChild(doc.createTextNode("2016-03-31T02:51:44"));    //Transaction credit date time will go here!

            //Acct tree
            Element Acct = doc.createElement("Acct");
            Ntfctn.appendChild(Acct);
            Element Id4 = doc.createElement("Id");
            Acct.appendChild(Id4);
            Element Othr2 = doc.createElement("Othr");
            Id4.appendChild(Othr2);
            Element Id5 = doc.createElement("Id5");
            Othr2.appendChild(Id5);
            Id5.appendChild(doc.createTextNode("41215212776"));     //Acct ID will go here!

            //TxsSummry tree
            Element TxsSummry = doc.createElement("TxsSummry");
            Ntfctn.appendChild(TxsSummry);
            Element TtlDbtNtries = doc.createElement("TtlDbtNtries");
            TxsSummry.appendChild(TtlDbtNtries);
            Element NbOfNtries = doc.createElement("NbOfNtries");
            TtlDbtNtries.appendChild(NbOfNtries);
            NbOfNtries.appendChild(doc.createTextNode("1"));    //Number of entires will go here!
            Element Sum = doc.createElement("Sum");
            TtlDbtNtries.appendChild(Sum);
            Sum.appendChild(doc.createTextNode("97.99"));       //Total sum will go here!


            //Possible loop will go here
            Element Ntry = doc.createElement("Ntry");
            Ntfctn.appendChild(Ntry);

            Element NtryRef = doc.createElement("NtryRef");
            Ntry.appendChild(NtryRef);
            NtryRef.appendChild(doc.createTextNode("163V24924AFW1LJ4"));    //Ntry Reference ID

            Element AmtCcy = doc.createElement("Amt Ccy");
            NtryRef.appendChild(AmtCcy);






            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("C:\\Users\\jhamric\\Desktop\\testing.xml"));
            transformer.transform(source, result);


        }catch (ParserConfigurationException pce){
            pce.printStackTrace();
        }catch (TransformerException tfe){
            tfe.printStackTrace();
        }

    }

}

I know that is a good bit of code but I thought by posting what I have so far you could understand what I have been able to do. I have no problem creating elements, assigning child elements and assigning a text value to those elements. The problem I am having now involves two different elements I need to create.

The first is the root element, I need to use a namespace like this:

+<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04">

instead of just having as I currently have.

I did find some answers like here

Issues with xpath that contained namespaces in Java (Dom parser)

that explain using an XPath however I can't really understand how to execute it with my code.

The second problem I have is a field like this:

<Amt Ccy="USD">94134.86</Amt>

As you can see it appears that the tag ="USD" and is also assigned a text node.

I tried doing this

Element AmtCcy = doc.createElement("Amt_Ccy='USD'");

but I get the following error..

Exception in thread "main" org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElement(Unknown Source)
    at creatingXML.CreateXML.main(CreateXML.java:103)
Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook
Picked up _JAVA_OPTIONS: -Xrunjvmhook -Xbootclasspath/a:"C:\Program Files (x86)\HP\Unified Functional Testing\bin\java_shared\classes";"C:\Program Files (x86)\HP\Unified Functional Testing\bin\java_shared\classes\jasmine.jar"

I would really appreciate any help with these two issues.

Community
  • 1
  • 1
jesric1029
  • 698
  • 3
  • 10
  • 33
  • What about adding attributes to your 'rootElement' setAttribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:camt.054.001.04"); ? – Tokazio Mar 31 '16 at 16:00

2 Answers2

3

You create a namespaced element by using createElementNs:

Element rootElement doc.createElementNS("urn:iso:std:iso:20022:tech:xsd:camt.054.001.04", 
       "Document");

Second <Amt Ccy="USD">94134.86</Amt> is a element Amt having an attribute Ccy with value USD and a text context of 94134.86. To create the attribute write:

Element AmtCcy = doc.createElement("Amty");
AmtCcy.setAttribute("Ccy", "USD");
NtryRef.appendChild(AmtCcy);
wero
  • 32,544
  • 3
  • 59
  • 84
  • Thanks - That is a fantastic answer. You described perfectly to me the difference between element, attribute, value and text context. That really helps me a ton! Also great answer for the root element. I was still thinking I needed to use XPath but as you shown it is not needed. – jesric1029 Mar 31 '16 at 17:17
1

For your second issue it's failing because createElement just takes a tag name as a parameter, you can't put expressions in there as all it will do is try to create a tag with literally that name. I'm not 100% sure what you're trying to do but I'm assuming Ccy is an attribute of the Element so you can do this

Element AmtCcy = doc.createElement("Amt");
AmtCcy.setAttribute("Ccy","USD");

You could use setAttribute to set your namespace as well if you wanted to.

rootElement.setAttribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:camt.054.001.04")

but you should use the createElementNS method of the document

Andrew Aitken
  • 671
  • 3
  • 14
  • Thanks for the answer. I picked the other because he went into a bit more detail but you also had a really good answer and I will upvote. – jesric1029 Mar 31 '16 at 17:18