我不确定如何在Stax中关闭此行为。当我解析文档时,我希望在解析它时捕获原始xml。
编辑以添加工作代码。
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.XMLEvent;
import org.apache.commons.lang.StringEscapeUtils;
public class Tester {
public static void main(String[] args) {
boolean output = false;
boolean pubDateMonthFlg = false;
boolean createDateFlg = false;
boolean createDateYrFlg = false;
String pubDateYear ="";
String pubDateMonth ="";
String createDateYear="";
StringBuilder k = new StringBuilder();
String file = "c:\\a.xml";
try{
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
Reader rd = new InputStreamReader(new FileInputStream(file),"UTF-8");
XMLEventReader reader = inputFactory.createXMLEventReader(rd);
long start = 0;
StringBuffer temp = new StringBuffer();
while (reader.hasNext()) {
XMLEvent event = (XMLEvent) reader.next();
//makes sure the xml is welformed
if (output == true) {
if (isIgnoreEvent(event)) {
//k.append(event.toString());
k.append(event.toString());
} else {
k.append(StringEscapeUtils.escapeXml(event.toString()));
}
}
if (event.getEventType() == event.START_ELEMENT) {
if (event.asStartElement().getName().getLocalPart().equalsIgnoreCase("xml")) {
k.append(event.toString());
output = true;
}
if (event.asStartElement().getName().getLocalPart().equalsIgnoreCase("node")) {
pubDateMonthFlg = true;
}
}
if (event.getEventType() == event.END_ELEMENT) {
if (event.asEndElement().getName().getLocalPart().equalsIgnoreCase("xml")) {
// the collected xml
System.out.println(k.toString());
//reset
k.delete(0, k.length());
output = false;
createDateFlg = false;
}
}
if (event.isCharacters()) {
Characters characters = (Characters) event;
if (!characters.isIgnorableWhiteSpace()&& !characters.isWhiteSpace()) {
//set the parent pubDateFlg to false
if(pubDateMonthFlg){
pubDateMonthFlg = false;
}
//piiFlg = false;
output = true;
}
}
}
reader.close();
}
catch (Exception e){
e.printStackTrace();
}
}
/**
* Ignore Start , End and comment Events
* @param event
* @return
*/
protected static boolean isIgnoreEvent(XMLEvent event){
return (event.isStartElement() || event.isEndElement() || event.getEventType() == event.COMMENT) ? Boolean.TRUE : Boolean.FALSE;
}
}
这就是我用作样本
<xml>
<node category="conclusion" value="reviewer's reason">blah blah blah!! </node>
</xml>
生成Stax
<xml>
<node category='conclusion' value='reviewer's reason'>blah blah blah!! </node>
</xml>
结果当你尝试另一个解析器尝试摄取输出时,它就变成了一个非常好的文档。
任何帮助表示赞赏 感谢
答案 0 :(得分:0)
我不太明白你要做什么......但考虑使用XMLEventWriter而不是StringBuilder和toString。