插入 CData XML 解析 Java 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14158023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:17:10  来源:igfitidea点击:

Inserting CData XML parsing Java String

javaxmlxml-parsingsax

提问by aspiringCoder

I have a java string containing XML. I want to read through this Java String wrap all the text nodes within CData, only I'm not sure how to do this. The reason for doing this is that the is a text node containing an angle bracket which is causing an exception when I try to parse the String. Can any1 help me out?

我有一个包含 XML 的 java 字符串。我想通读这个 Java String 包装 CData 中的所有文本节点,只是我不知道如何做到这一点。这样做的原因是这是一个包含尖括号的文本节点,当我尝试解析字符串时会导致异常。任何人都可以帮助我吗?

<node> this < is text <node> <node2> this is < text <node2>

I would like to know if there is an easy way of reading this text as a string with XMLReader and inserting CData around the text

我想知道是否有一种简单的方法可以使用 XMLReader 作为字符串读取此文本并在文本周围插入 CData

thanks

谢谢

Stefan

斯蒂芬

回答by Woot4Moo

Perhaps something like this (apologies in advance for any inefficiency:

也许是这样的(提前为任何低效道歉:

if(currentNode instanceof XMLNodeType.Text)  
{  
     String toWrite = String.format("<![CDATA[%s]]>", currentNode.getText());   
     // or whatever retrieves text of the node
}  

It looks like you need to massage the data to be valid XML. The process for this is of course highly dependent on your input. So essentially what occurs is you receive a big string that you need to convert into valid XML. The advantage here is that you can define a schema that the third party adheres to, this is a meeting with them so it is outside of the scope of discussion, but is worth mentioning. Once you have this schema defined you will know which nodes are considered "text" nodes and need to be wrapped in CDATAblocks.

看起来您需要将数据处理为有效的 XML。这个过程当然高度依赖于您的输入。所以基本上发生的事情是您收到一个大字符串,您需要将其转换为有效的 XML。这里的优点是您可以定义第三方遵守的模式,这是与他们的会面,因此不在讨论范围内,但值得一提。一旦你定义了这个模式,你就会知道哪些节点被认为是“文本”节点并且需要被包裹在CDATA块中。

The basic idea is this:

基本思想是这样的:

List<String> textTags = new ArrayList<String>();  
textTags.add("NODE");  
//other things to add
String bigAwfulString = inputFromThirdParty();   
String validXML = ""; 
for(String currentNode : bigAwfulString.split("yourRegexHere")  
{  
    if(textTags.contains(currentNode)  
    {  
           validXML+=String.format("<![CDATA[%s]]>", currentNode.getText());    
           continue;
    }   
    validXML+=currentNode;
}

回答by andorker

Try this, it worked for me.
http://www.java2s.com/Code/Java/XML/AddingaCDATASectiontoaDOMDocument.htm

试试这个,它对我有用。
http://www.java2s.com/Code/Java/XML/AddingaCDATASectiontoaDOMDocument.htm

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;

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

public class Main {
  public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");

    // Add a CDATA section to the root element
    element = doc.getDocumentElement();
    CDATASection cdata = doc.createCDATASection("data");
    element.appendChild(cdata);

  }
}