我试图将jquery ajax中的xml数据发送到spring控制器并尝试解析java中的xml。但是无法继续下去。
请你帮我解决这个问题 -
这是我正在尝试的 -
示例XMl -
<Company>
<Employees>
<Employee>
<ID>1</ID>
<Name>
<fname>
Anand
</fname>
<lname>
Chavan
</lname>
</Name>
</Employee>
<Employee>
<ID>2</ID>
<Name>
<fname>
Rahul
</fname>
<lname>
Dravid
</lname>
</Name>
</Employee>
</Employees>
</Company>
如果您的观察结果是xml字符串中缺少以下代码 - (请注意,我已尝试过两个..有和没有下面的节点)
<?xml version=\""+"1.0"+"\" ?>
示例Ajax请求 -
function saveAjaxCall(xml){
xml=$(xml).html();
$.ajax({
url : "bulkAdd.ext",
type : "POST",
processData: false,
dataType : "text/xml",
data: 'data=' + xml,
success : function(response){
alert('Success');
},
error : function(response) {
alert('Failed');
}
});
}
控制器 -
@RequestMapping(value = "/bulkAdd.ext", method = RequestMethod.POST)
public @ResponseBody String addBulkEmp( @RequestBody String xml) throws Exception {
new EmpXMLParser().parseXML(xml);
// parser is not complete, initially just trying to count number of nodes
return "";
}
示例解析器代码 -
public void parseXML(String xml) {
try {
System.out.println(xml);
InputStream is = new ByteArrayInputStream(Charset.forName("UTF-8")
.encode(xml).array());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
db = dbf.newDocumentBuilder();
Document dom = db.parse(is);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getChildNodes();
if (nl != null && nl.getLength() > 0) {
System.out.println("number of Emp = " + nl.getLength());
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException saxe) {
// TODO Auto-generated catch block
saxe.printStackTrace();
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
}
}
我遇到错误 -
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
at com.testcode.myapp.utils.EmployeeXMLParser.parseXML(EmployeeXMLParser.java:33)
at com.testcode.myapp.controller.EmployeeController.addEmployee(EmployeeController.java:205)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:629)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:590)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:874)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:790)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)
观察 -
控制器以下列格式接收数据 -
data=%3Cemployees%3E%3Cemployee%3E%3Cid%3E1%3C%2Fid%3E%3Cname%3E%3Cfname%3EAnand%3C%2Ffname%3E%3Clname%3EChavan%3C%2Flname%3E%3C%2Fname%3E%3C%2Femployee%3E%3Cemployees%3E%3Cemployee%3E%3Cid%3E2%3C%2Fid%3E%3Cname%3E%3Cfname%3ERahul%3C%2Ffname%3E%3Clname%3EDravid%3C%2Flname%3E%3C%2Fname%3E%3C%2Femployee%3E%3C%2Femployees%3E%3C%2Femployees%3E
等待 - 如果您觉得数据=在传递数据时出现问题 - 我也试过以下方式
data: {data : xml }
或
data:xml,
仍然失败。
掌握思想请大家伸出援助之手。
答案 0 :(得分:0)
我几天前刚遇到同样的问题。 UTF-8在内容之前使用特殊符号,而XmlReader在第一行XML之前不期望任何数据。
因此,要处理它,您应该使用此方法处理您的String:
public static final String UTF8_BOM = "\uFEFF";
private static String removeUTF8BOM(String s) {
if (s.startsWith(UTF8_BOM)) {
s = s.substring(1);
}
return s;
}