Extracting XML fragments

R

Roger Varley

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
 
T

Toddy Marx

Roger said:
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
 
R

Roger Varley

Toddy Marx said:
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
 
T

Toddy Marx

Roger said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top