powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / oracle.xml.parser.v2.*;
4 сообщений из 4, страница 1 из 1
oracle.xml.parser.v2.*;
    #33290298
Фотография _AndreyP
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
есть у кого пример как
парсить входящий xml в "Windows-1251"?
...
Рейтинг: 0 / 0
oracle.xml.parser.v2.*;
    #33290549
Фотография _AndreyP
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
спотыкается на : parser.parse(DemoUtil.createURL(argv[0]).toString());
Error(53,27): variable DemoUtil not found in class mypackage1.SAXSample

пример из мануала, чего не хватает ? или DemoUtil нужно как-то обьявить ?

Код: 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.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
/* Copyright (c) Oracle Corporation 2000, 2001. All Rights Reserved. */

/**
 * DESCRIPTION
 * This file demonstates a simple use of the parser and SAX API.
 * The XML file that is given to the application is parsed and 
 * prints out some information about the contents of this file.
 */

 import  java.net.URL;

 import  org.xml.sax.Parser;
 import  org.xml.sax.Locator;
 import  org.xml.sax.AttributeList;
 import  org.xml.sax.HandlerBase;
 import  org.xml.sax.InputSource;
 import  org.xml.sax.SAXException;
 import  org.xml.sax.SAXParseException;

 import  oracle.xml.parser.v2.SAXParser;

 public   class  SAXSample  extends  HandlerBase
{
   // Store the locator
   Locator locator;

    static   public   void  main(String[] argv)
   {
       try 
      {
          if  (argv.length !=  1 )
         {
            // Must pass in the name of the XML file.
            System.err.println("Usage: SAXSample filename");
            System.exit( 1 );
         }
         // Create a new handler for the parser
         SAXSample sample =  new  SAXSample();

         // Get an instance of the parser
         Parser parser =  new  SAXParser();
         
         // set validation mode
         ((SAXParser)parser).setValidationMode(SAXParser.DTD_VALIDATION);
         // Set Handlers in the parser
         parser.setDocumentHandler(sample);
         parser.setEntityResolver(sample);
         parser.setDTDHandler(sample);
         parser.setErrorHandler(sample);
    
         // Convert file to URL and parse
          try 
         {
            parser.parse(DemoUtil.createURL(argv[ 0 ]).toString());
         }
          catch  (SAXParseException e) 
         {
            System.out.println(e.getMessage());
         }
          catch  (SAXException e) 
         {
            System.out.println(e.getMessage());
         }  
      }
       catch  (Exception e)
      {
         System.out.println(e.toString());
      }
   }

   //////////////////////////////////////////////////////////////////////
   // Sample implementation of DocumentHandler interface.
   //////////////////////////////////////////////////////////////////////

    public   void  setDocumentLocator (Locator locator)
   {
      System.out.println("SetDocumentLocator:");
       this .locator = locator;
   }

    public   void  startDocument() 
   {
      System.out.println("StartDocument");
   }

    public   void  endDocument()  throws  SAXException 
   {
      System.out.println("EndDocument");
   }
      
    public   void  startElement(String name, AttributeList atts) 
                                                   throws  SAXException 
   {
      System.out.println("StartElement:"+name);
       for  ( int  i= 0 ;i<atts.getLength();i++)
      {
         String aname = atts.getName(i);
         String type = atts.getType(i);
         String value = atts.getValue(i);

         System.out.println("   "+aname+"("+type+")"+"="+value);
      }
      
   }

    public   void  endElement(String name)  throws  SAXException 
   {
      System.out.println("EndElement:"+name);
   }

    public   void  characters( char [] cbuf,  int  start,  int  len) 
   {
      System.out.print("Characters:");
      System.out.println( new  String(cbuf,start,len));
   }

    public   void  ignorableWhitespace( char [] cbuf,  int  start,  int  len) 
   {
      System.out.println("IgnorableWhiteSpace");
   }
   
   
    public   void  processingInstruction(String target, String data) 
               throws  SAXException 
   {
      System.out.println("ProcessingInstruction:"+target+" "+data);
   }
   

      
   //////////////////////////////////////////////////////////////////////
   // Sample implementation of the EntityResolver interface.
   //////////////////////////////////////////////////////////////////////


    public  InputSource resolveEntity (String publicId, String systemId)
                       throws  SAXException
   {
      System.out.println("ResolveEntity:"+publicId+" "+systemId);
      System.out.println("Locator:"+locator.getPublicId()+" "+
                  locator.getSystemId()+
                  " "+locator.getLineNumber()+" "+locator.getColumnNumber());
       return   null ;
   }

   //////////////////////////////////////////////////////////////////////
   // Sample implementation of the DTDHandler interface.
   //////////////////////////////////////////////////////////////////////

    public   void  notationDecl (String name, String publicId, String systemId)
   {
      System.out.println("NotationDecl:"+name+" "+publicId+" "+systemId);
   }

    public   void  unparsedEntityDecl (String name, String publicId,
         String systemId, String notationName)
   {
      System.out.println("UnparsedEntityDecl:"+name + " "+publicId+" "+
                         systemId+" "+notationName);
   }

   //////////////////////////////////////////////////////////////////////
   // Sample implementation of the ErrorHandler interface.
   //////////////////////////////////////////////////////////////////////


    public   void  warning (SAXParseException e)
               throws  SAXException
   {
      System.out.println("Warning:"+e.getMessage());
   }

    public   void  error (SAXParseException e)
               throws  SAXException
   {
       throw   new  SAXException(e.getMessage());
   }


    public   void  fatalError (SAXParseException e)
               throws  SAXException
   {
      System.out.println("Fatal error");
       throw   new  SAXException(e.getMessage());
   }
}
...
Рейтинг: 0 / 0
oracle.xml.parser.v2.*;
    #33290601
Фотография Denis Popov
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
DemoUtil должен идти там же, в примерах

Код: 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.
 import  java.io.File;
 import  java.net.MalformedURLException;
 import  java.net.URL;

 public   class  DemoUtil {

  // Helper method to create a URL from a file name
   public   static  URL createURL(String fileName) {
    URL url =  null ;
     try  {
      url =  new  URL(fileName);
    }
     catch  (MalformedURLException ex) {
      File f =  new  File(fileName);
       try  {
        String path = f.getAbsolutePath();
        // This is a bunch of weird code that is required to
        // make a valid URL on the Windows platform, due
        // to inconsistencies in what getAbsolutePath returns.
        String fs = System.getProperty("file.separator");
         if  (fs.length() ==  1 ) {
           char  sep = fs.charAt( 0 );
           if  (sep != '/') {
            path = path.replace(sep, '/');
          }
           if  (path.charAt( 0 ) != '/') {
            path = '/' + path;
          }
        }
        path = "file://" + path;
        url =  new  URL(path);
      }
       catch  (MalformedURLException e) {
        System.out.println("Cannot create url for: " + fileName);
        System.exit( 0 );
      }
    }
     return  url;
  }
}

Но ИМХО для разбора документов в Cp1251 этого не достаточно, у меня он вылетел с сообщением:

Код: plaintext
Exception in thread "main" java.io.UTFDataFormatException: Invalid UTF8 encoding.


Попробуй передавать ему не URL, а Reader, при создании которого указывай кодировку.
Код: plaintext
1.
2.
3.
4.
5.
6.
//      parser.parse(DemoUtil.createURL(argv[0]).toString());
File file =  new  File(argv[ 0 ]);
InputStreamReader in =  new  InputStreamReader(
   new  BufferedInputStream( new  FileInputStream(file)), "Cp1251"
);
parser.parse( new  InputSource(in));
...
Рейтинг: 0 / 0
oracle.xml.parser.v2.*;
    #33292925
Фотография _AndreyP
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
пасиб!
а то в доке что-то не получилось найти DemoUtil(смотрю Oracle® XML Developer's Kit Programmer's Guide 10g Release 1 (10.1))
может глаза кривые:)
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / oracle.xml.parser.v2.*;
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


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