DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Join Date
    Jan 2009
    Location
    India
    Posts
    1

    StAX and XMLBeans

    hi
    i am facing a problem while trying to apply the stax parser and xmlbeans technologies together to parse the xml files.
    THe exception i encountered and the code which throws it are as shown below.
    waitng for a quick reply by any of you
    Thanks in advance...
    Code:
    Exception :
    Exception in thread "main" org.apache.xmlbeans.XmlException: error: Unexpected element: OPEN_TAG
    	at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3486)
    	at org.apache.xmlbeans.impl.store.Locale.parse(Locale.java:712)
    	at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:696)
    	at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:683)
    	at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:208)
    	at mydocument$Factory.parse(Unknown Source)
    	at Mysrc.EFetchSTAXBeansParser.parse(STAXBeansParser.java:82)
    	at Mysrc.StAXEutils.sendFetchRequest(StAXEutils.java:66)
    	at Mysrc.StAXEutils.main(StAXEutils.java:85)
    Caused by: org.xml.sax.SAXParseException: Unexpected element: OPEN_TAG
    	at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportFatalError(Piccolo.java:1038)
    	at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:723)
    	at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3454)
    	... 8 more
    
    
    here's the code throwing xmlexception...
    
    
     static mydocumentType parse(InputStream fetchin) throws FileNotFoundException, XMLStreamException, javax.xml.stream.XMLStreamException, XmlException
         {    	 
    	  XMLInputFactory inFactory = XMLInputFactory.newInstance();
    	  //File filenew=new File("xxx.xmll");
    	  //FileReader filereader=new FileReader(filenew);	 
    	  XMLEventReader reader = inFactory.createXMLEventReader(fetchin);
          XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
          ResettableStringWriter swriter =
              new ResettableStringWriter(1 * 1024 * 1024);      
          XMLEventWriter writer = outFactory.createXMLEventWriter(swriter);
          boolean inFetchRec = false;    
          while (reader.hasNext())
          {
        	  XMLEvent evt = reader.nextEvent();
        	  if (evt.isStartElement())
        	  {
    	          StartElement elem = (StartElement) evt;	          
    	          if(elem.getName().getLocalPart().equalsIgnoreCase("mystartelement"))
      		  	  {
    	        	inFetchRec = false;
      		  	  }
    	          if(elem.getName().getLocalPart().equalsIgnoreCase("myreqelement"))
    	          {
    	        	  inFetchRec = true;
    	          }
    	      }
        	  if (inFetchRec)
        	  {
        		writer.add(evt);
        	  }
        	  if (evt.isEndElement())
        	  {
        		  EndElement elemt = (EndElement) evt;    		  
        		  if(elemt.getName().getLocalPart().equalsIgnoreCase("mystartelement"))
        		  {
        			  inFetchRec = true;
        		  }
        		  else if(elemt.getName().getLocalPart().equalsIgnoreCase("myreqelement"))
        		  {
        			  inFetchRec= false;
        		  }
        	  }
          }
          writer.flush();
          String str=swriter.reset();
          System.out.println(str);
          mydocument Ob= mydocument.Factory.parse(str);
          return ob;
    }
    
    
    the xml looks like this...
    
    <mystartelement>
                 <myreqelement>
                       <ddd>ksdjfksjdfg</ddd>
                       <dfgfg>sdfgsdhkgfsd</dfgfg>
                 </myreqelement>
                 <myreqelement>
                       <ddd>ksdjfkfdgggggggggggggggggsjdfg</ddd>
                       <dfgfg>sdfgsdhkgfssdffffffffffffffffd</dfgfg>
                 </myreqelement>
                 <myreqelement>
                       <ddd>ksdjfksjdfg</ddd>
                       <dfgfg>sdfgsdhkgfsd</dfgfg>
                 </myreqelement>
                 <myreqelement>
                       <ddd>ksdjfksjdsdddddddddddddddfg</ddd>
                       <dfgfg>sdfgssddfffffffffffffffffffffdhkgfsd</dfgfg>
                 </myreqelement>
    </mystartelement>
    Note:
    "mydocument" is the type generated from the XMLBEANS ...which am using to parse the xmlfragment or event iam writing to the eventwriter using STAX API initially.
    The code worked fine when i tried to collect the main parent element in the xml...mystartelement.and parse it using xmlbeans.
    Last edited by Hack; 01-15-2009 at 07:31 AM. Reason: Added Code Tags

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links