java中的DOM解析器不编码UTF-8中的引号

时间:2012-05-04 05:27:22

标签: java xml parsing dom

我正在尝试使用本教程中提供的代码:http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/

我也粘贴了下面的代码,似乎编码了所有预定义字符的问题<,>和&等等但不是单引号或双引号(“和”)。我真的很感激修复。下面的代码还有一个编辑,使得生成的xml格式正确

更具体地说:

     import java.io.File;
   import javax.xml.parsers.DocumentBuilder;
       import javax.xml.parsers.DocumentBuilderFactory;
     import javax.xml.parsers.ParserConfigurationException;
        import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.dom.DOMSource;
        import javax.xml.transform.stream.StreamResult;

       import org.w3c.dom.Attr;
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;

 public class WriteXMLFile {

public static void main(String argv[]) {

  try {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // root elements
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("company");
    doc.appendChild(rootElement);

    // staff elements
    Element staff = doc.createElement("Staff");
    rootElement.appendChild(staff);

    // set attribute to staff element
    Attr attr = doc.createAttribute("id");
    attr.setValue("1");
    staff.setAttributeNode(attr);

    // shorten way
    // staff.setAttribute("id", "1");

    // firstname elements
    Element firstname = doc.createElement("firstname");
    firstname.appendChild(doc.createTextNode("yong"));
    staff.appendChild(firstname);

    // lastname elements
    Element lastname = doc.createElement("lastname");
    lastname.appendChild(doc.createTextNode("mook kim"));
    staff.appendChild(lastname);

    // nickname elements
    Element nickname = doc.createElement("nickname");
    nickname.appendChild(doc.createTextNode("mkyong"));
    staff.appendChild(nickname);

    // salary elements
    Element salary = doc.createElement("salary");
    salary.appendChild(doc.createTextNode("100000"));
    staff.appendChild(salary);

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("C:\\file.xml"));

    // Output to console for testing
    // StreamResult result = new StreamResult(System.out);

    transformer.transform(source, result);

    System.out.println("File saved!");

  } catch (ParserConfigurationException pce) {
    pce.printStackTrace();
  } catch (TransformerException tfe) {
    tfe.printStackTrace();
  }
}

}

2 个答案:

答案 0 :(得分:0)

我认为您的代码运行正常。在属性值中加上双引号,看看会发生什么。

答案 1 :(得分:0)

阅读XML规范的section 2.4。生成14的语法

[14]    CharData       ::=      [^<&]* - ([^<&]* ']]>' [^<&]*)

告诉您字符数据可以是除“&lt;”之外的任何(有效的XML)字符和'&amp;' (或']]&gt;'序列)。尽管建议使用“&gt;”,但并非绝对必要。