Stream to String in Jdom
How to preserve white space in jdom?
i get the below error:
org.jdom.JDOMException: Error on line 1: Content is not allowed in prolog
This is my code, kindly tell the solution for this code...
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.lang.*;
public class JDOM_XML_TEST {
Document m_doc = null;
public Element elmEHResponse = null;
String sEHResponseXml = "";
String sOperationType = "Edit";
static String s_return = "";
StringBuffer strbuffer = null;
String sParamsXml_1 = "<VWPortal>" + "<EHResponse Type='" + " test CustomObject" + "' ProgLangId='" + "1" + "'>" + "<Signature>public void Item1_ButtonClick_RESP1(Context ObjContextParam, string sPortlet, string sPortletInstanceId, CILPrtlDataset objCILPrtlDataset)</Signature></EHResponse><ResultType>Result</ResultType></VWPortal>";
//String sParamsXml ="public String Display_Test(){" "String s ='Welcome';" "return s; }";
public static void main(String[] args) {
JDOM_XML_TEST xml_test = new JDOM_XML_TEST();
s_return = xml_test.getXml();
//System.out.println("Value is:::"+s_return);
}
public String getXml() {
try {
if (!sOperationType.equalsIgnoreCase("Delete")) {
SAXBuilder builder = new SAXBuilder(false);
//Commended for CustomObject
//doc = builder.build(new ByteArrayInputStream(sParamsXml.getBytes("UTF-8")));
//For CustomObject Alignment
strbuffer = new StringBuffer();
strbuffer.append("public String Display_Test(){ \n");
开发者_如何学JAVA strbuffer.append("\t\t\tString s ='Welcome'; \n");
strbuffer.append("\t\t\t return s; \n");
strbuffer.append("}");
sEHResponseXml = strbuffer.toString();
System.out.println("Value of sEHResponseXml:::" + sEHResponseXml);
builder.setIgnoringElementContentWhitespace(true);
m_doc = builder.build(new ByteArrayInputStream(sEHResponseXml.getBytes("UTF-8")));
elmEHResponse = m_doc.getRootElement();
System.out.println("Value of elmEHResponse in before:::" + elmEHResponse);
XMLOutputter Out = new XMLOutputter();
sEHResponseXml = Out.outputString(elmEHResponse);
System.out.println("Value of elmEHResponse in after:::" + sEHResponseXml);
}
} catch (Exception e) {
e.printStackTrace();
}
return sEHResponseXml;
}
}
This error is not related to preserving whitespace in your XML but some extra bytes before the start of your xml content. Use NotePad++ (or BBEdit etc) and check if there any extra characters at the start of your xml doc. Also post your XML here.
This is my code, kindly tell the solution for this code...
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.lang.*;
public class JDOM_XML_TEST {
Document m_doc = null;
public Element elmEHResponse = null;
String sEHResponseXml = "";
String sOperationType = "Edit";
static String s_return = "";
StringBuffer strbuffer = null;
String sParamsXml_1 = "<VWPortal>" + "<EHResponse Type='" + " test CustomObject" + "' ProgLangId='" + "1" + "'>" + "<Signature>public void Item1_ButtonClick_RESP1(Context ObjContextParam, string sPortlet, string sPortletInstanceId, CILPrtlDataset objCILPrtlDataset)</Signature></EHResponse><ResultType>Result</ResultType></VWPortal>";
//String sParamsXml ="public String Display_Test(){" "String s ='Welcome';" "return s; }";
public static void main(String[] args) {
JDOM_XML_TEST xml_test = new JDOM_XML_TEST();
s_return = xml_test.getXml();
//System.out.println("Value is:::"+s_return);
}
public String getXml() {
try {
if (!sOperationType.equalsIgnoreCase("Delete")) {
SAXBuilder builder = new SAXBuilder(false);
//Commended for CustomObject
//doc = builder.build(new ByteArrayInputStream(sParamsXml.getBytes("UTF-8")));
//For CustomObject Alignment
strbuffer = new StringBuffer();
strbuffer.append("public String Display_Test(){ \n");
strbuffer.append("\t\t\tString s ='Welcome'; \n");
strbuffer.append("\t\t\t return s; \n");
strbuffer.append("}");
sEHResponseXml = strbuffer.toString();
System.out.println("Value of sEHResponseXml:::" + sEHResponseXml);
builder.setIgnoringElementContentWhitespace(true);
m_doc = builder.build(new ByteArrayInputStream(sEHResponseXml.getBytes("UTF-8")));
elmEHResponse = m_doc.getRootElement();
System.out.println("Value of elmEHResponse in before:::" + elmEHResponse);
XMLOutputter Out = new XMLOutputter();
sEHResponseXml = Out.outputString(elmEHResponse);
System.out.println("Value of elmEHResponse in after:::" + sEHResponseXml);
}
} catch (Exception e) {
e.printStackTrace();
}
return sEHResponseXml;
}
}
Your Problem is that in your code you are creating a StringBuffer strbuffer
//For CustomObject Alignment
strbuffer = new StringBuffer();
strbuffer.append("public String Display_Test(){ \n");
strbuffer.append("\t\t\tString s ='Welcome'; \n");
strbuffer.append("\t\t\t return s; \n");
strbuffer.append("}");
This StringBuffer has the contents
public String Display_Test(){ String s ='Welcome'; return s; }
You are assigning that StringBuffer to the String sEHResponseXml
and printing it to the console
sEHResponseXml = strbuffer.toString();
System.out.println("Value of sEHResponseXml:::" + sEHResponseXml);
The problem is that the String sEHResponseXml
does not contain valid it XML (it is not XML at all). You then try and construct a JDOM document m_doc
from this string
builder.setIgnoringElementContentWhitespace(true);
m_doc = builder.build(new ByteArrayInputStream(sEHResponseXml.getBytes("UTF-8")));
And this is where you program fails, telling you
org.jdom.JDOMException: Error on line 1: Content is not allowed in prolog
I re-ran your program but changed strbuffer
to contain valid XML (<doc> <p> h1 </p> </doc>
), here is the program below
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.lang.*;
public class JDOM_XML_TEST {
Document m_doc = null;
public Element elmEHResponse = null;
String sEHResponseXml = "";
String sOperationType = "Edit";
static String s_return = "";
StringBuffer strbuffer = null;
String sParamsXml_1 = "<VWPortal>" + "<EHResponse Type='" + " test CustomObject" + "' ProgLangId='" + "1" + "'>" + "<Signature>public void Item1_ButtonClick_RESP1(Context ObjContextParam, string sPortlet, string sPortletInstanceId, CILPrtlDataset objCILPrtlDataset)</Signature></EHResponse><ResultType>Result</ResultType></VWPortal>";
//String sParamsXml ="public String Display_Test(){" "String s ='Welcome';" "return s; }";
public static void main(String[] args) {
JDOM_XML_TEST xml_test = new JDOM_XML_TEST();
s_return = xml_test.getXml();
//System.out.println("Value is:::"+s_return);
}
public String getXml() {
try {
if (!sOperationType.equalsIgnoreCase("Delete")) {
SAXBuilder builder = new SAXBuilder(false);
//Commended for CustomObject
//doc = builder.build(new ByteArrayInputStream(sParamsXml.getBytes("UTF-8")));
//For CustomObject Alignment
strbuffer = new StringBuffer();
strbuffer.append("<doc> <p> h1 </p> </doc>");
sEHResponseXml = strbuffer.toString();
System.out.println("Value of sEHResponseXml:::" + sEHResponseXml);
builder.setIgnoringElementContentWhitespace(true);
m_doc = builder.build(new ByteArrayInputStream(sEHResponseXml.getBytes("UTF-8")));
elmEHResponse = m_doc.getRootElement();
System.out.println("Value of elmEHResponse in before:::" + elmEHResponse);
XMLOutputter Out = new XMLOutputter();
sEHResponseXml = Out.outputString(elmEHResponse);
System.out.println("Value of elmEHResponse in after:::" + sEHResponseXml);
}
} catch (Exception e) {
e.printStackTrace();
}
return sEHResponseXml;
}
}
This outputs
Value of sEHResponseXml:::<doc> <p> h1 </p> </doc>
Value of elmEHResponse in before:::[Element: <doc/>]
Value of elmEHResponse in after:::<doc> <p> h1 </p> </doc>
精彩评论