
February 25th, 2005, 04:06 PM
|
|
Registered User
|
|
Join Date: Feb 2005
Posts: 1
Time spent in forums: 1 h 10 m 58 sec
Reputation Power: 0
|
|
|
more MalformedURLException problems
Quote: | Originally Posted by bricker42 You can't parse an xml string using the same method that you use to parse an xml file. If you try using the same method java will try to interpret the xml string as a url and you will get the error you're seeing.
If you're working with xml in java I'd recommend using dom4j. It's fast and has the easiest to use API that I've found. It has a DocumentHelper.parse() function that will take an xml string and return a Document. If you use straight JAXP I think you'll need to wrap the xml string in a StringReader. |
I'm having a similar (I think) problem. I have written an XSLT which works fine using it in XMLSpy 2005 when the source XML is in a local file along with the DTD file (ops.dtd) in the same directory. However, the problem is that I will actually receive the XML in a J2EE application as a string parsed from an HTTP response. When I try to transform the XML string (see below) I get "java.net.MalformedURLException: no protocol"
XML Listing:
Code:
<?xml version='1.0' encoding="UTF-8" standalone="no" ?>
<!DOCTYPE OPS_envelope SYSTEM "ops.dtd">
<OPS_envelope>
<header>
<version>0.9</version>
</header>
<body>
<data_block>
<dt_assoc>
<item key="object">DOMAIN</item>
<item key="attributes">
<dt_assoc>
<item key="status">available</item>
<item key="match"></item>
</dt_assoc>
</item>
<item key="response_text">Domain available</item>
<item key="is_success">1</item>
<item key="response_code">210</item>
<item key="protocol">XCP</item>
<item key="action">REPLY</item>
</dt_assoc>
</data_block>
</body>
</OPS_envelope>
If I modify the XML string so that the DTD URI is "file:C:/Projects/TUCOWS/ops.dtd" (my local absolute path to a copy of the dtd) then all is well - my transformation works and execution proceeds as expected. However, I consider this "fix" to be a less than desirable hack since what I really want is a way to either tell the parser loaded by the xalan transformer not to validate (so it won't need or care about the DTD) or a better way to specify where to find the DTD. I am using the xercesImpl-2.1.0.jar and using the following "locally grown" methods to do the transform:
Code:
public static String transformString(String xmlString, String xslURL) throws XMLServerException
{
StreamSource xmlSource = null;
StreamSource xslSource = null;
try
{
xmlSource = new StreamSource(new StringReader(xmlString) );
xslSource = new StreamSource(new URL(xslURL).openStream());
return transformReader(xmlSource, xslSource);
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
protected static String transformReader(StreamSource xmlSource, StreamSource xslSource) throws XMLServerException
{
StringWriter out = new StringWriter();
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslSource);
transformer.transform(xmlSource, new StreamResult(out));
}
catch (TransformerFactoryConfigurationError tfce)
{
tfce.printStackTrace();
throw new XMLServerException(XMLServerException.TRANSFORMER_CONFIGURATION,
"Transformation Configuration exception: " + tfce.toString());
}
catch (TransformerException e)
{
e.printStackTrace();
throw new XMLServerException(XMLServerException.TRANSFORMATION,
"Transformation exception: " + e.toString());
}
return out.toString();
}
The XML in the listing above is the first argument to transformString(), the second argument is the local absolute URL to the following XSLT:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:dns="http://www.hns.com/iag/framework/xml/tucows/domainsrvc"
xmlns="http://www.hns.com/iag/framework/xml/tucows/domainsrvc/lookup"
exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- ~~~~~~~~~~~~~~~~~~ LookupDomainResponseMsg ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<xsl:template match="/OPS_envelope">
<LookupDomainResponseMsg>
<xsl:attribute name="xsi:schemaLocation">http://www.hns.com/iag/framework/xml/tucows/domainsrvc/lookup C:/Projects/TUCOWS/domainsrvc_lookup.xsd</xsl:attribute>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<responseHeader>
<xsl:for-each select="body">
<xsl:for-each select="data_block">
<xsl:for-each select="dt_assoc">
<xsl:for-each select="item">
<xsl:choose>
<xsl:when test="@key = 'object'">
<object>
<xsl:value-of select="."/>
</object>
</xsl:when>
<xsl:when test="@key = 'protocol'">
<protocol>
<xsl:value-of select="."/>
</protocol>
</xsl:when>
<xsl:when test="@key = 'action'">
<action>
<xsl:value-of select="."/>
</action>
</xsl:when>
<xsl:when test="@key = 'response_code'">
<responseCode>
<xsl:value-of select="."/>
</responseCode>
</xsl:when>
<xsl:when test="@key = 'response_text'">
<responseText>
<xsl:value-of select="."/>
</responseText>
</xsl:when>
<xsl:when test="@key = 'is_success'">
<isSuccess>
<xsl:value-of select="."/>
</isSuccess>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</responseHeader>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<responseAttributes>
<xsl:for-each select="body">
<xsl:for-each select="data_block">
<xsl:for-each select="dt_assoc">
<xsl:for-each select="item">
<xsl:if test="@key = 'attributes'">
<xsl:for-each select="dt_assoc">
<xsl:for-each select="item">
<xsl:choose>
<xsl:when test="@key = 'status'">
<status>
<xsl:value-of select="."/>
</status>
</xsl:when>
<xsl:when test="@key = 'price_status'">
<priceStatus>
<xsl:value-of select="."/>
</priceStatus>
</xsl:when>
<xsl:when test="@key = 'no_service'">
<noService>
<xsl:value-of select="."/>
</noService>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</responseAttributes>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
</LookupDomainResponseMsg>
</xsl:template>
</xsl:stylesheet>
Thanks in advance for your help!
Bob
|