Special characters creates problem while writing xml

Simratpal singh :

first of all please excuse my shallow understanding into coding as I am a business analyst. Now my question. I am writing java code to convert a csv into xml. I am able to read csv successfully into objects. However, while writing the xml, when special a space or "=" is encounteredan error is thrown.

Piece of the problematic code, I have imporovised the value in create element just to highlight the problem. In actual I am getting this value from an object:-

DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document xmlDocument= documentBuilder.newDocument();

Element root = xmlDocument.createElement("Media NationalGroupId="8" AllFTA="1002" AllSTV="1001");

xmlDocument.appendChild(root);

My xml should look something like this

<Media DateCreated="20200224 145251" NationalGroupId="8" AllFTA="1002" AllSTV="1001" AllTV="1000" NextId="1000000">
Evert :

createElement should only receive Media as the argument.

To add the other attributes (DateCreated, NationalGroupId, etc), you need to call setAttribute on root, one by one.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238095&siteId=1