Question

I want to parse XML from a web service using JSTL. The XML contains namespaces that results in problems with parsing and outputting results

XML string:

<MonthlyPayments:paymentsSummary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:MonthlyPayments="http://www.zillow.com/static/xsd/MonthlyPayments.xsd" xsi:schemaLocation="http://www.zillow.com/static/xsd/MonthlyPayments.xsd http://www.zillowstatic.com/vstatic/LATEST/static/xsd/MonthlyPayments.xsd">
  <request>
    <price>100000</price>
    <down>15</down>
    <zip>98104</zip>
  </request>
  <payment loanType="thirtyYearFixed">
    <rate>4.2</rate>
    <monthlyPrincipalAndInterest>416</monthlyPrincipalAndInterest>
    <monthlyMortgageInsurance>31</monthlyMortgageInsurance>
  </payment>
</MonthlyPayments:paymentsSummary>

JSP file (resultString contains the XML):

<c:set var="xmldocument">${map.resultString}</c:set>    
<x:parse var="doc" xml="${xmldocument}"  />
 ...
<x:out select="$doc/MonthlyPayments/request/price" /> 

When removing the paymentSummary part in the XML the output is correct as 1000000. I need to be able to parse the XML containing the namespace. Please help?

Was it helpful?

Solution

I managed to find a solution that works for me.

<b>MonthlyPayments > request > Price </b>:                 
<x:out select="$doc//*[name()='request']/*[name()='price']"/>
<br>    

MonthlyPayments > request > Price : 100000

<b>MonthlyPayments > response > payment > rate </b>:                 
<x:out select="$doc//*[name()='response']/*[name()='payment']/*[name()='rate']"/>
<br>                     

MonthlyPayments > response > payment : 4.2

<b>MonthlyPayments > response > payment > loantype </b>:                         
<x:out select="$doc//*[name()='response']/*[name()='payment']/@loanType"/>

MonthlyPayments > response > payment > loantype : thirtyYearFixed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top