Problems while saving a xml file

edited May 2018 in How To...

Hi everyone, as the title suggest, i'm having an issue when i try to save a xml file.

basically the problem is that when i try to save a xml file with the function "saveXML()" it changes some chars, for example, if a variable has the char "&" it gets changed with "& amp;".

Any idea on what is happening?

(Mod edit, broke the html entity so it's visible)

Answers

  • So & becomes & ?

  • edited May 2018

    The forum / html is probably changing the & amp; in the post to an ampersand. (It was, I changed the question to make it visible)

    The XML is doing the opposite - ampersands need escaping in XML.

  • koogs explained it better than me, thanks. I thought about encoding, how should i convert it?

        String decodificado = new String(byte(saving[y][6].toCharArray()), java.nio.charset.Charset.forName("ISO-8859-1"));
        part.setString("CabCode", decodificado);  //código de barras (código)
    

    i've tryed with that code (and changed the ISO-8859-1 with UTF-8) but didn't work (saving[][] is the variable where i have the string with those characters).

  • edited May 2018 Answer ✓

    well, the "solution" that i found is the following, first i save the XML file, then i open it as an string array and search for things like "& amp;" "& lt;" "& gt;" (without the spaces) and i replace them with "&", "<" and ">" witch are special chars for the XML file, after all that i save the file, the code is the following:

          saveXML(etiquetas, "data/" + nombreArchivo);
    
          String[] postproceso = loadStrings("data/" + nombreArchivo);
          for(int i = 0; i < postproceso.length; i++)
          {
            postproceso[i] = postproceso[i].replaceAll("&amp;", "&").replaceAll("&lt;", "<").replaceAll("&gt;", ">");
          }
          saveStrings("data/" + nombreArchivo, postproceso);
    

    i don't like this "solution" because i dont know if there is another special char that i have to consider. If someone have an eleganter solution it would be welcomed

  • if you save it as xml then load it as xml. the problem is that you're reading it back as strings.

  • I don't load it, the xml file has to be opened with another program that prints barcodes

  • edited May 2018

    It is possible that your barcode reader is broken, and cannot read valid XML.

    HOWEVER, this is a valid XML file:

    <?xml version="1.0" encoding="UTF-8"?>
    <value>&amp;</value>
    

    It lists a single value, "&". That value is correctly escaped.

    This is an invalid, broken XML file:

    <?xml version="1.0" encoding="UTF-8"?>
    <value>&</value>
    

    It generates an error because it lists a broken entity.

    Go ahead and try it!

    and you will see that the second example fails validation:

    error on line 2 at column 9: xmlParseEntityRef: no name

    If you leave the output of saveXML() alone, it will create correct, valid XML. If you mess with it by loading and saving strings as you describe it will break the XML character encoding.

  • (i've looked and the processing implementation of xml doesn't support cdata blocks)

  • @jeremydouglass you are right, it turns into a broken xml file, so it isn't a solution.

  • Great! So is just using saveXML() working for you, and does that mean your problem is resolved?

Sign In or Register to comment.