users@jaxb.java.net

Re: how to marshal an object to document fragment

From: <zzhao_at_CS.UNO.EDU>
Date: Tue, 28 Jan 2003 04:12:39 -0700

My arkward solution was to use string manipulation. BTW if <?xml ... ?> is after some whitespace, ie, not the first several characters the unmarshaller see, an exception will occur.

      String str = logs[i];
      //remove xml prolog <?xml...?>
      indexa = str.indexOf("?>");
      if (indexa!=-1) str = str.substring(indexa+2);

      //remove xsi and xmlns attributes that are unrecongnizable to oracle parser
      index = str.indexOf(">"); //seperate tag head from contents
      String head = str.substring(0, index+1);
      indexa = -1;
      do
      {
        indexa = head.indexOf("xsi:");
        if (indexa!=-1)
        {
          indexb = head.indexOf("=\"", indexa);
          indexb = head.indexOf("\"", indexb+2);
          head = head.substring(0, indexa-1) + head.substring(indexb+1);
        }
        indexa = head.indexOf("xmlns:");
        if (indexa!=-1)
        {
          indexb = head.indexOf("=\"", indexa);
          indexb = head.indexOf("\"", indexb+2);
          head = head.substring(0, indexa-1) + head.substring(indexb+1);
        }
      }
      while (indexa!=-1);
      logstrs[i] = head + str.substring(index+1); //logstrs contain filtered strings



Gary Gregory <ggregory_at_seagullsw.com> wrote:

>Actually, how would one simply strip the <?xml ...?> PI?
>
>>From the SAX Javadoc on ContentHandler: A SAX parser must never report an
>XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0,
>section 4.3.1) using this method.
>
>I have sevaral model objects that I need to serialize into /one/ XML
>document. I am happy to create the enclosing document with the top <?xml...>
>and a root element, but each time I marshal an object, I do must not have
>the <?xml...>
>
>Arg,
>Gary
>
>-----Original Message-----
>From: Ryan Shoemaker - JavaSoft East [mailto:Ryan.Shoemaker_at_Sun.COM]
>Sent: Monday, January 27, 2003 2:29 PM
>To: JAXB-INTEREST_at_JAVA.SUN.COM
>Subject: Re: how to marshal an object to document fragment
>
>
>zhidong zhao wrote:
> > How to set the marshaller to marshal a globe type into document fragment
>string without xml
> > prolog or xsi:xxx xmlns:xxx attributes?
> >
>
>There is no way to do this via the javax.xml.bind interfaces. We considered
>adding
>this feature to the 1.0 version of JAXB, but ultimately removed it. You
>should be
>able to marshal to a custom ContentHandler that strips the start|endDocument
>events
>out of the SAX stream.
>
>--Ryan Shoemaker, Sun Microsystems, Inc.
>