I am trying to create an xml document to send via a POST request. However, I am getting this error on the server side :
[org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
I think it may be because I am missing
<?xml version="1.0" encoding="UTF-8"?>
However I am unsure on the correct way to implement it. If anyone has any ideas that would great !
export function jsToXML(film, crudOp) {
//Creating Document and elements
var xmlDoc = document.implementation.createDocument(null, "film");
const xmlVersion = '<?xml version="1.0"?>';
var title = xmlDoc.createElement("title");
var year = xmlDoc.createElement("year");
var director = xmlDoc.createElement("director");
var stars = xmlDoc.createElement("stars");
var review = xmlDoc.createElement("review");
//Edit & Delete CRUD Operations need ID
if (crudOp === "edit" || crudOp === "delete") {
var id = xmlDoc.createElement("id");
id.append(film.id);
xmlDoc.documentElement.appendChild(id);
}
//Assign form data to elements
title.append(film.title);
year.append(film.year);
director.append(film.director);
stars.append(film.stars);
review.append(film.review);
//Append the elements to the xml doc
xmlDoc.documentElement.appendChild(title);
xmlDoc.documentElement.appendChild(year);
xmlDoc.documentElement.appendChild(director);
xmlDoc.documentElement.appendChild(stars);
xmlDoc.documentElement.appendChild(review);
return xmlDoc;
}
CodePudding user response:
There's a very wide variety of possible causes: see org.xml.sax.SAXParseException: Content is not allowed in prolog for some of them. Omitting the XML declaration isn't one of those causes.
This is generally a message from the Java XML parser, so I'm not sure where your Javascript code (for constructing an XML tree) fits in.
CodePudding user response:
To fix this I added a serializer as .createElement(Item), creates a HTML Node.
//Serialize xml dom
var serializer = new XMLSerializer();
var xmlString = serializer.serializeToString(xmlDoc);
return xmlString;
This is what I added to solve the problem