Java 为Web服务响应转义整个XML

Java 为Web服务响应转义整个XML,java,xml,web-services,rest,jakarta-ee,Java,Xml,Web Services,Rest,Jakarta Ee,我有一个Java Restful Web服务,它返回userdetails xml,如下所示: <userdetails> <firstName>first</firstName> <firstName>last</firstName> <email>123@gmail.com</email> </userdetails> 但我必须对XML中的每个字段都这样做。是否有任何方法

我有一个Java Restful Web服务,它返回userdetails xml,如下所示:

<userdetails>
    <firstName>first</firstName>
    <firstName>last</firstName>
    <email>123@gmail.com</email>
</userdetails>

但我必须对XML中的每个字段都这样做。是否有任何方法可以一次转义整个XML,而不是对每个字段都转义。

另一种方法是遍历所有文本元素,在下面的过程中转义它们代码取自并稍加修改:

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang.StringEscapeUtils;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class EscapeXml {
  static String inputFile = "data.xml";
  static String outputFile = "data_new.xml";

  public static void main(String[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder().parse(new InputSource(inputFile));

    // locate the node(s)
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList)doc.getElementsByTagName("*");

    // escape text nodes
    for (int idx = 0; idx < nodes.getLength(); idx++) {
      Node node = nodes.item(idx);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList childNodes = node.getChildNodes();
        for (int cIdx = 0; cIdx < childNodes.getLength(); cIdx++) {
          Node childNode = childNodes.item(cIdx);
          if (childNode.getNodeType() == Node.TEXT_NODE) {
            String newTextContent =  
              StringEscapeUtils.escapeXml(childNode.getTextContent());                
            childNode.setTextContent(newTextContent);
          }
        }
      }
    }

    // save the result
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
  }
}

任何xml编码器都应该自动为您处理转义。您一直在使用jaxb进行编组和解组…在服务器端它没有抱怨…但在客户端,在解组过程中注意到具有这些特殊字符的字段的问题什么问题?客户端是如何解析xml的?客户端也在使用jaxb…所以当他们试图将xml解组为java对象时,他们看到了这些特殊字符的问题。他们看到了什么问题?
import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang.StringEscapeUtils;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class EscapeXml {
  static String inputFile = "data.xml";
  static String outputFile = "data_new.xml";

  public static void main(String[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance()
              .newDocumentBuilder().parse(new InputSource(inputFile));

    // locate the node(s)
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList)doc.getElementsByTagName("*");

    // escape text nodes
    for (int idx = 0; idx < nodes.getLength(); idx++) {
      Node node = nodes.item(idx);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList childNodes = node.getChildNodes();
        for (int cIdx = 0; cIdx < childNodes.getLength(); cIdx++) {
          Node childNode = childNodes.item(cIdx);
          if (childNode.getNodeType() == Node.TEXT_NODE) {
            String newTextContent =  
              StringEscapeUtils.escapeXml(childNode.getTextContent());                
            childNode.setTextContent(newTextContent);
          }
        }
      }
    }

    // save the result
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
  }
}