Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Modifying a UTF-16 XML document

807580Mar 4 2010 — edited Mar 9 2010
Hey guys,

I need to modify the configuration file for the EventLog Inspector. the file is settings.elicfg. It is a UTF-16 encoded document.

Originally I wrote java code that successfully parsed, modified, and wrote the document in UTF-8. Then I realized that the ELI will not work for UTF-8, so I have to go back to UTF-16. However, when I try to do this, the output files are just gibberish. Does anyone know what I'm doing wrong??

public static void verifyGeneralSettings (int value)
      throws Exception
  {
    //TODO make sure value is boolean 1 or 0.
    //retrieve the filepath of settings.clifg
    String filePath = parseConfigFile(MASTER_CONFIG_PATH, SETTINGS_FILE_NODE_NAME);
    try
    {
      //Retrieve settings.clifg xml file
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new File(filePath));
      Element rootElement = doc.getDocumentElement();
      NodeList eventLogInspectorNodes = rootElement.getElementsByTagName(GENERAL_NODE_NAME);

      //Find the general node.
      if (eventLogInspectorNodes == null || eventLogInspectorNodes.getLength() != 1)
      {
        log.error(GENERAL_NODE_ERROR);
        throw new Exception(IMPROPER_XML_FORMAT);
      }
      Node generalNode = eventLogInspectorNodes.item(0);
      Element generalElement = (Element)generalNode;

      //verify CheckUpdates, HideTaskbar, and TrayIcon
      verifySpecificNode(generalElement, CHECKUPDATES_NODE_NAME, value);
      verifySpecificNode(generalElement, HIDETASKBAR_NODE_NAME, value);
      verifySpecificNode(generalElement, TRAY_ICON_NODE_NAME, value);

      //prepare to output to file
      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
      transformer.setOutputProperty(OutputKeys.METHOD, "xml");

      //initialize StreamResult with File object to save to file
      // Prepare the output file
      File file = new File(filePath);
      Result result = new StreamResult(file);

      //Write to file
      DOMSource source = new DOMSource(doc);
      transformer.transform(source, result);

      if (log.isInfoEnabled())
      {
        log.info("CheckUpdates changed to " + value);
      }
    }
    catch(IOException ioe)
    {
      log.error(ioe.getStackTrace(), ioe);
      throw new Exception(ioe);
    }
    catch(ParserConfigurationException pce)
    {
      log.error(pce.getStackTrace(), pce);
      throw new Exception(pce);
    }
    catch(SAXException saxe)
    {
      log.error(saxe.getStackTrace(), saxe);
      throw new Exception(saxe);
    }
    catch(Exception e)
    {
      log.error(e.getStackTrace(), e);
      throw new Exception(e);
    }
  }
I have tried comment out the setproperty(OutputKeys.ENCODING, "UTF-16" line, and am a little out of ideas. I recieve no errors or exception during compile time or run-time, so I'm not sure why it's failing. I had expected to recieve a "content not allowed in prolog" if there was some sort of encoding conflict between what I write and the file that I'm writing to, but I do not recieve that error either. Following is the header created if I simply change "UTF-16" to "UTF-8" in the code above:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<EventLogInspector>
  <Version>210</Version>
  <Timestamp>1267739308</Timestamp>
however just switching to UTF-16 provides the gibberish (looks like korean letters, and boxes)

I think that my code is reading IN the file as UTF-8, and perhaps that is part of the problem. (I know this because I'll get the "prolog error" unless I manually change the header to UTF-8 before I run my program. How do I tell it to read as UTF-16??? I can post more of the code if needed, I just didn't want to clutter up the post with unneccessary code. I'm pretty sure that this method has all of the critical elements. Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 6 2010
Added on Mar 4 2010
16 comments
1,710 views