6
다양한 HTML 태그가있는 html 형식의 문자열이 있습니다. html 태그가 그대로 유지되도록 xml 태그에이 문자열을 넣고 싶습니다. 예 :xml에 html 태그 저장
public class XMLfunctions {
public final static Document XMLfromString(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
/** Returns element value
* @param elem element (it is XML tag)
* @return Element value otherwise empty String
*/
public final static String getElementValue(Node elem) {
Node kid;
if(elem != null){
if (elem.hasChildNodes()){
for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()){
if(kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
}
}
}
return "";
}
/*Start Parsing Body */
public static String getBodyXML(String id){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.44:9090/solr/core0/select/?q=content_id:"+id+"&version=2.2&start=0&rows=10&indent=on");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
String st= ParseXMLBodyNode(line,"doc");
return st;
}
public static String ParseXMLBodyNode(String str,String node){
String xmlRecords = str;
String results = "";
String[] result = new String [1];
StringBuffer sb = new StringBuffer();
StringBuffer text = new StringBuffer();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
NodeList indiatimes1 = doc.getElementsByTagName(node);
sb.append("<results count=");
sb.append("\"1\"");
sb.append(">\r\n");
for (int i = 0; i < indiatimes1.getLength(); i++) {
Node node1 = indiatimes1.item(i);
if (node1.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node1;
NodeList nodelist = element.getElementsByTagName("str");
Element element1 = (Element) nodelist.item(0);
NodeList title = element1.getChildNodes();
title.getLength();
for(int j=0; j<title.getLength();j++){
text.append(title.item(j).getNodeValue());
}
System.out.print((title.item(0)).getNodeValue());
sb.append("<result>\r\n");
sb.append("<body>");
String tmpText = html2text(text.toString());
sb.append("<![CDATA[<body>");
sb.append(tmpText);
sb.append("</body>]]>");
sb.append("</body>\r\n");
sb.append("</result>\r\n");
result[i] = title.item(0).getNodeValue();
}
}
sb.append("</results>");
} catch (Exception e) {
System.out.println("Exception........"+results);
e.printStackTrace();
}
return sb.toString();
}
/*End Parsing Body*/
public static int numResults(Document doc){
Node results = doc.getDocumentElement();
int res = -1;
try{
res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
}catch(Exception e){
res = -1;
}
return res;
}
public static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return XMLfunctions.getElementValue(n.item(0));
}
public static String html2text(String html) {
String pText = Jsoup.clean(html, Whitelist.basic());
return pText;
}
}
나는 내가 글꼴 태그는 XML에서 HTML 태그로이있을 수 있음을 원하는
String xml = XMLfunctions.getBodyXML(id);
Document doc = XMLfunctions.XMLfromString(xml);
같은이 함수를 호출합니다.
도움을 주시면 감사하겠습니다 !!!!!
@BoltClock : 그것은 다음과 같습니다, 그래서 어쩌면 당신은 그 두 줄을 제거해야합니다 sb.append (tmpText); sb.append ("]]>"); 또는 기타 – ReNa
이해가 안됩니다. 'StringBuilder' 또는 무엇인가를 사용하여 XML을 구성하고 있습니까? – BoltClock
@BoltClock : 먼저 jsoup basic() 메서드를 사용하여 XML을 구문 분석합니다. 모든 서식을 문자열로 유지 한 다음 해당 문자열을 StringBuffer에 추가합니다. 이전 주석에서 물어 본 방식으로이 XML이 다시 나타납니다. 파싱 된 현재로서는 cdata를 사용한 후에도 아무 것도 얻지 못합니다. – ReNa