0
I have an XML document that serves for a program in Java save some values that will be read by another in C#. It’s not the best way to exchange values between languages (that’s what happened, but I accept alternative suggestions), but it works relatively well, the problem is that when I update XML values, sometimes (apparently randomly), at the end of the XML text appears the symbol ">", and I have no idea why, and this causes the following error:
[Fatal Error] test.xml:1:200: Content is not allowed in trailing section.
The error-free XML is as follows: (I idented XML for ease*)
<?xml version="1.0" encoding="ASCII" standalone="no"?>
<Data>
<v1>0</v1>
<v2>0</v2>
<v3>0</v3>
<v4>60</v4>
<avg>60</avg>
<lvl1>70</lvl1>
<lvl2>85</lvl2>
<lvl3>100</lvl3>
<lvl4>115</lvl4>
<hRate>75</hRate>
</Data>
Since what has the error stays like this:
<?xml version="1.0" encoding="ASCII" standalone="no"?>
<Data>
<v1>0</v1>
<v2>0</v2>
<v3>0</v3>
<v4>60</v4>
<avg>60</avg>
<lvl1>70</lvl1>
<lvl2>85</lvl2>
<lvl3>100</lvl3>
<lvl4>115</lvl4>
<hRate>75</hRate>
</Data>> //ERRO NESTA LINHA
The part of the code that updates XML is this:
public static void UpdateValuesXML(XMLParametter... param)
{
try {
String tempDir = System.getProperty(property); //find windows temp folder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(tempDir + fileName);
for (int i = 0; i < param.length; i++)
{
Node val = doc.getElementsByTagName(XMLValue(param[i].param)).item(0);
val.setTextContent(Integer.toString(param[i].value));
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(tempDir + fileName));
transformer.transform(source, result);
} catch (Exception ex) {}
}
There is a >, on the line that accuses the error: </Date>> //ERROR ON THIS LINE
– Edjane
yes, this is the problem, this > appears after a while the program is running, I do not know why it appears, because at first it does not appear, when generates the XML file this as in the first example (without this > more) and the program runs normally, but after a while (which seems to be random) this last > arises
– Roberto Gomes
I’m sorry, I didn’t quite understand the problem. I’m surprised, because from what I understand, it doesn’t always happen, right? What you could do is treat this information before it is updated, like replacing >> with > if there is.
– Edjane
I thought about doing it, but I don’t know and I haven’t found how to do it in this case
– Roberto Gomes