CSV를 XML로 변환 한 다음 OutputStream으로 변환해야합니다. 규칙은 "
을 내 코드의 "
으로 변환하는 것입니다.자바에서 정확한 xml 이스케이프
입력 CSV 행 :
{"Test":"Value"}
예상 출력 :
<root>
<child>{"Test":"Value"}</child>
<root>
전류 출력 :
<root>
<child>{&quot;Test&quot;:&quot;Value&quot;}</child>
<root>
코드 :
File file = new File(FilePath);
BufferedReader reader = null;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document newDoc = domBuilder.newDocument();
Element rootElement = newDoc.createElement("root");
newDoc.appendChild(rootElement);
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
Element rowElement = newDoc.createElement("child");
rootElement.appendChild(rowElement);
text = StringEscapeUtils.escapeXml(text);
rowElement.setTextContent(text);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(newDoc);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
System.out.println(new String(baos.toByteArray()))
도와 주시겠습니까? 무엇이 그리울 때 &
은 &
로 변환 되나요?
이중 탈출구입니다. DOM이 당신을 위해 탈출하지만, 당신도 탈출합니다. 'StringEscapeUtils.escapeXml (text)'에 대한 호출을 제거하십시오. – Andreas
나는 이것에 대해 읽었습니다. 이상한 것은 탈출을 제거한 후에 탈출이 전혀 발생하지 않는다는 것입니다. – user3305630
'''로 인용 된 값을 갖는 속성에서만'''을 이스케이프해야하기 때문에. 이 유효한 XML입니다 : '<테스트 foo는 = "이라고 말했다 것 : " 안녕하세요 ""바 ='그가 ' d는 말했다 : "안녕하세요" '> 그는 말했다 것입니다 : "안녕하세요"'. '>'는']]'(CDATA 터미네이터']]>')를 따를 때만 따옴표를 써야하지만'>'는''''와''''는 항상 인용 부호로 묶어야합니다 (CDATA 제외) 항상 항상 인용. – Andreas