Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Java [игнор отключен] [закрыт для гостей] / русские буквы в XML - Illegal XML character / 5 сообщений из 5, страница 1 из 1
23.11.2004, 10:42
    #32795372
sanitar
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
русские буквы в XML - Illegal XML character
Перечитал все что есть в форуме по поводу русских символов в jave ниче не помогло. Может подскажите что надо поправить в коде, чтобы Transformer нормально обрабатывал XML с русскими значениями атрибутов?
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
   public   void  doGet(HttpServletRequest req, HttpServletResponse resp)
       throws  ServletException, IOException
 {

        req.setCharacterEncoding("Cp1251");
        resp.setContentType("text/html;charset=Windows-1251");
        HashMap h=  new  HashMap();
        h = getServletParameters(req);
        Iterator it = h.keySet().iterator();

         if  (DBC.ok)  {
          Statement stmt =  null ;
          Connection c = DBC.getConn();

             try  { 
             sXML = "<?xml version="1. 0 " encoding="WINDOWS- 1251 "?>"+
                     "<REPORT title=\"Заголовок русскими буквами\"><KSU_BLOCK>"+
                     "<KSU><TIMEKEY>4</TIMEKEY>"+
                     "<P_OIL_IN>1.4368877</P_OIL_IN><T_OIL_IN>8.0301604</T_OIL_IN></KSU></KSU_BLOCK></REPORT>";
 
             StringBufferInputStream strXML=  new  StringBufferInputStream(sXML);

             StringBufferInputStream strXML = bd.getDataStream();


             Source xmlSource =  new  StreamSource(strXML);
             String path= this .getServletContext().getRealPath("/");
             Source xsltSource =  new  StreamSource(path+"XSL/ksu.xsl");


             Result result =  new  StreamResult(resp.getOutputStream());
             // get the factory
             TransformerFactory transFact = TransformerFactory.newInstance();

             // get a transformer for this particular stylesheet
             Transformer trans = transFact.newTransformer(xsltSource);

             trans.setOutputProperty(OutputKeys.METHOD,"html");

             trans.setOutputProperty(OutputKeys.ENCODING,"Windows-1251");
            // do the transformation
             trans.transform(xmlSource, result);
           }
            catch  (Exception ex) {
              throw   new  ServletException(ex);
             }

          }
         else 
        {
         PrintWriter out = resp.getWriter();
         out.println("<HTML>");
         out.println("<a href=\"http://"+req.getServerName()+":"+req.getServerPort()+"/index.jsp\">back to main page </a>");
         out.println("<hr></hr>");
         out.println("<strong>Error:Connection failed!</strong>");
         out.println("<strong>Check your login and password</strong>");
         out.println("<hr></hr>");
         out.println("</HTML>");
         out.flush();
         }
        }
...
Рейтинг: 0 / 0
23.11.2004, 10:46
    #32795382
sanitar
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
русские буквы в XML - Illegal XML character
Вот такой вот код - не все вычистил изначально. Смотрите на него а не то что выше.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
   public   void  doGet(HttpServletRequest req, HttpServletResponse resp)
       throws  ServletException, IOException
 {

        req.setCharacterEncoding("Cp1251");
        resp.setContentType("text/html;charset=Windows-1251");
        HashMap h=  new  HashMap();
        h = getServletParameters(req);
        Iterator it = h.keySet().iterator();

         if  (DBC.ok)  {
          Statement stmt =  null ;
          Connection c = DBC.getConn();

             try  { // Это преобразование XML строки с помощью XSL файла
             sXML = "<?xml version=\"1.0\" encoding=\"WINDOWS-1251\"?>"+
                     "<REPORT title=\"Заголовок русскими буквами\"><KSU_BLOCK>"+
                     "<KSU><TIMEKEY>4</TIMEKEY>"+
                     "<P_OIL_IN>1.4368877</P_OIL_IN><T_OIL_IN>8.0301604</T_OIL_IN></KSU></KSU_BLOCK></REPORT>";
 
             StringBufferInputStream strXML=  new  StringBufferInputStream(sXML);

             Source xmlSource =  new  StreamSource(strXML);
             String path= this .getServletContext().getRealPath("/");
             Source xsltSource =  new  StreamSource(path+"XSL/ksu.xsl");


             Result result =  new  StreamResult(resp.getOutputStream());
             // get the factory
             TransformerFactory transFact = TransformerFactory.newInstance();

             // get a transformer for this particular stylesheet
             Transformer trans = transFact.newTransformer(xsltSource);

             trans.setOutputProperty(OutputKeys.METHOD,"html");

             trans.setOutputProperty(OutputKeys.ENCODING,"Windows-1251");
            // do the transformation
             trans.transform(xmlSource, result);
           }
            catch  (Exception ex) {
              throw   new  ServletException(ex);
             }

          }
         else 
        {
         PrintWriter out = resp.getWriter();
         out.println("<HTML>");
         out.println("<a href=\"http://"+req.getServerName()+":"+req.getServerPort()+"/index.jsp\">back to main page </a>");
         out.println("<hr></hr>");
         out.println("<strong>Error:Connection failed!</strong>");
         out.println("<strong>Check your login and password</strong>");
         out.println("<hr></hr>");
         out.println("</HTML>");
         out.flush();
         }
        }
...
Рейтинг: 0 / 0
23.11.2004, 10:59
    #32795410
Gurney
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
русские буквы в XML - Illegal XML character
sanitarВот такой вот код - не все вычистил изначально. Смотрите на него а не то что выше.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
             sXML = "<?xml version=\"1.0\" encoding=\"WINDOWS-1251\"?>"+
                     "<REPORT title=\"Заголовок русскими буквами\"><KSU_BLOCK>"+
                     "<KSU><TIMEKEY>4</TIMEKEY>"+
                     "<P_OIL_IN>1.4368877</P_OIL_IN><T_OIL_IN>8.0301604</T_OIL_IN></KSU></KSU_BLOCK></REPORT>";
 
             StringBufferInputStream strXML=  new  StringBufferInputStream(sXML);

             Source xmlSource =  new  StreamSource(strXML);


Класс StringBufferInputStream не может правильно обрабатывать нац. символы. Используйте StringReader.
...
Рейтинг: 0 / 0
23.11.2004, 11:53
    #32795550
sanitar
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
русские буквы в XML - Illegal XML character
Подскажите как правильно сделать то? попробовал так, transform вылетает с эксепшном.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
   public   void  doGet(HttpServletRequest req, HttpServletResponse resp)
       throws  ServletException, IOException
 {

        req.setCharacterEncoding("cp1251");
        resp.setContentType("text/html;charset=cp1251");
        HashMap h=  new  HashMap();
        h = getServletParameters(req);

        Iterator it = h.keySet().iterator();
        String parName =  null ;
         while  (it.hasNext()) {
          parName = (String)it.next();
          System.out.println(parName+"="+(String)h.get(parName));
        }

         if  (DBC.ok)  {
          Statement stmt =  null ;
          Connection c = DBC.getConn();

             try  { 
             sXML = "<?xml version="1. 0 " encoding="WINDOWS- 1251 "?>"+
                     "<REPORT title=\"Заголовок русскими буквами\"><KSU_BLOCK>"+
                     "<KSU><TIMEKEY>4</TIMEKEY>"+
                     "<P_OIL_IN>1.4368877</P_OIL_IN><T_OIL_IN>8.0301604</T_OIL_IN></KSU></KSU_BLOCK></REPORT>";
                int  b;
               ByteArrayOutputStream baos =  new  ByteArrayOutputStream();
               StringReader sr =  new  StringReader(sXML);
                while ( (b = sr.read()) != - 1  )
               {
               baos.write(b);
               }
               String s = baos.toString("Cp1251");

//               Source xmlSource = new StreamSource(new ByteArrayInputStream(baos.toByteArray()));
               Source xmlSource =  new  StreamSource(s);

             String path= this .getServletContext().getRealPath("/");
             Source xsltSource =  new  StreamSource(path+"XSL/ksu.xsl");

             Result result =  new  StreamResult(resp.getOutputStream());
             // get the factory
             TransformerFactory transFact = TransformerFactory.newInstance();

             // get a transformer for this particular stylesheet
             Transformer trans = transFact.newTransformer(xsltSource);

             trans.setOutputProperty(OutputKeys.METHOD,"html");
             trans.setOutputProperty(OutputKeys.INDENT, "yes" );
             trans.setOutputProperty(OutputKeys.ENCODING,"cp1251");
            // do the transformation
             trans.transform(xmlSource, result);
           }
            catch  (Exception ex) {
              throw   new  ServletException(ex);
             }

          }
         else 
        {
         PrintWriter out = resp.getWriter();
         out.println("<HTML>");
         out.println("<a href=\"http://"+req.getServerName()+":"+req.getServerPort()+"/index.jsp\">back to main page </a>");
         out.println("<hr></hr>");
         out.println("<strong>Error:Connection failed!</strong>");
         out.println("<strong>Check your login and password</strong>");
         out.println("<hr></hr>");
         out.println("</HTML>");
         out.flush();
         }

        }
      

...
Рейтинг: 0 / 0
23.11.2004, 12:00
    #32795570
sanitar
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
русские буквы в XML - Illegal XML character
Может эту часть как-то по-другому заделать?
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
                int  b;
               ByteArrayOutputStream baos =  new  ByteArrayOutputStream();
               StringReader sr =  new  StringReader(sXML);
                while ( (b = sr.read()) != - 1  )
               {
               baos.write(b);
               }
               String s = baos.toString("Cp1251");

//               Source xmlSource = new StreamSource(new ByteArrayInputStream(baos.toByteArray()));
               Source xmlSource =  new  StreamSource(s);
...
Рейтинг: 0 / 0
Форумы / Java [игнор отключен] [закрыт для гостей] / русские буквы в XML - Illegal XML character / 5 сообщений из 5, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]