Extracting XML fragments

Discussion in 'Java' started by Roger Varley, Sep 2, 2003.

  1. Roger Varley

    Roger Varley Guest

    Hi

    I have an application that is passing my program a String that contains XML
    data. My program needs to examine the values of two elements within this
    string which it uses to identify the file "type" and it's "destination" and
    then forwards the string to the relevant handler. Is there an API which
    would allow me to search the xml data in the string for just those values
    that I'm interested in rather than incurring the overhead of building a DOM
    or writing an XMLReader class to parse the data? I'm lead to believe that
    Xerces will allow me to use an XPath expression but that this is specific to
    Xerces and I'd prefer a parser-agnostic soloution if possible.

    Regards
    Roger
     
    Roger Varley, Sep 2, 2003
    #1
    1. Advertising

  2. Roger Varley

    Toddy Marx Guest

    Roger Varley wrote:
    > Is there an API which
    > would allow me to search the xml data in the string for just those values
    > that I'm interested in rather than incurring the overhead of building a DOM
    > or writing an XMLReader class to parse the data?


    You could use an XSLT template to return you the desired value from your
    XML. Within the template you could point to the value with an XPath
    expressions.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text" version="1.0" encoding="UTF-8" />
    <xsl:template match="/path/to/your/node">
    <xsl:value-of select="." />
    </xsl:template>
    </xsl:stylesheet>


    In your program get a javax.xml.transform.Transformer object with your
    XSLT and let it transform your XML String.

    // StreamSource to pass XML string to the transformer
    StreamSource xml = getStreamSourceFromString(xml);
    // a new StringWriter for the result
    StringWriter sw = new StringWriter();

    // factory to instantiate a transformer
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // instantiate a Transformer with your stylesheet
    Transformer transformer;
    transformer = tFactory.newTransformer(new StreamSource(xslt));

    // transform and pass result to a StringWriter
    transformer.transform(xml, new StreamResult(sw));

    // the result
    String valueOfTheNode = sw.toString();


    Hope this helps!

    ~Toddy

    --
    Toddy Marx (email contains no z)
     
    Toddy Marx, Sep 2, 2003
    #2
    1. Advertising

  3. Roger Varley

    Roger Varley Guest

    "Toddy Marx" <> wrote in message
    news:bj2iv1$b9q$-online.net...
    > Roger Varley wrote:
    > > Is there an API which
    > > would allow me to search the xml data in the string for just those

    values
    > > that I'm interested in rather than incurring the overhead of building a

    DOM
    > > or writing an XMLReader class to parse the data?

    >
    > You could use an XSLT template to return you the desired value from your
    > XML. Within the template you could point to the value with an XPath
    > expressions.
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="text" version="1.0" encoding="UTF-8" />
    > <xsl:template match="/path/to/your/node">
    > <xsl:value-of select="." />
    > </xsl:template>
    > </xsl:stylesheet>
    >
    >
    > In your program get a javax.xml.transform.Transformer object with your
    > XSLT and let it transform your XML String.
    >
    > // StreamSource to pass XML string to the transformer
    > StreamSource xml = getStreamSourceFromString(xml);
    > // a new StringWriter for the result
    > StringWriter sw = new StringWriter();
    >
    > // factory to instantiate a transformer
    > TransformerFactory tFactory = TransformerFactory.newInstance();
    >
    > // instantiate a Transformer with your stylesheet
    > Transformer transformer;
    > transformer = tFactory.newTransformer(new StreamSource(xslt));
    >
    > // transform and pass result to a StringWriter
    > transformer.transform(xml, new StreamResult(sw));
    >
    > // the result
    > String valueOfTheNode = sw.toString();
    >


    Hi Toddy

    Thanks for the suggestion. Where do I find the method
    getStreamSourceFromString() - google can't find any reference for it.

    Regards
    Roger
     
    Roger Varley, Sep 3, 2003
    #3
  4. Roger Varley

    Toddy Marx Guest

    Roger Varley wrote:
    > Thanks for the suggestion. Where do I find the method
    > getStreamSourceFromString() - google can't find any reference for it.


    Hello Roger,

    good to hear that XSLT might work for you. Here is the method to get
    a StreamSource from a String:

    public StreamSource getStreamSourceFromString(String xml){
    ByteArrayInputStream baiStream = null;
    baiStream = new ByteArrayInputStream(xml.getBytes());
    StreamSource xmlInputAsString = new StreamSource(baiStream);
    return xmlInputAsString;
    }

    ~Toddy

    --
    Toddy Marx (email contains no z)
     
    Toddy Marx, Sep 4, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luigi Donatello Asero

    Fragments broken

    Luigi Donatello Asero, Jul 19, 2004, in forum: HTML
    Replies:
    0
    Views:
    470
    Luigi Donatello Asero
    Jul 19, 2004
  2. Hugh Sparks
    Replies:
    0
    Views:
    406
    Hugh Sparks
    Jul 30, 2004
  3. Ksenia Marasanova

    extracting HTML fragments and counting words

    Ksenia Marasanova, Feb 18, 2005, in forum: Python
    Replies:
    0
    Views:
    310
    Ksenia Marasanova
    Feb 18, 2005
  4. Jared  Zimmerman

    syndicate fragments of RSS feed?

    Jared Zimmerman, Sep 1, 2006, in forum: HTML
    Replies:
    8
    Views:
    472
    Jared Zimmerman
    Sep 9, 2006
  5. Peter Makholm
    Replies:
    2
    Views:
    189
    Permostat
    Mar 12, 2010
Loading...

Share This Page