0

I have a pseudo XML format that I get from an attribute into the session.

So I am triying to print it in a javascript Variable:

  var delitos = '<%=(String)request.getSession().getAttribute("delitosXML")%>';

But I get an error in the HTML structure, because it is printed like this:

       delitos = 

       '<listaCatalogo>
       <catDelitoDTO>
       <catDelitoId>1</catDelitoId>
        <claveDelito>1</claveDelito>
       <nombre>Violación</nombre>
       <esGrave>true</esGrave>
       </catDelitoDTO>
      <catDelitoDTO>
      <catDelitoId>3</catDelitoId>
          <claveDelito>3</claveDelito>
            <nombre>Daño en propiedad ajena</nombre>
           <esGrave>false</esGrave>
          </catDelitoDTO>
            </listaCatalogo>';

After that I want to find every element with jQuery like this:

  delitos.find('catDelitoDTO').each(function(){
    something();
   }

So I think I have to receive that XML inline to make it work. Is there a way to transform that pseudo XML in a String but inline?

Braj
  • 46,415
  • 5
  • 60
  • 76

2 Answers2

0

JSTL XML Tag Library

It is used to work with XML data used in the JSP page. The XML tag library helps in parsing, selecting and transforming XML data in JSP page. These operations can be done by using the XPath expressions with XML document. The XML tag library can be used with JSP page by using following tag library :

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

Read more and find sample code here.


Sample code:

Here it is iterating all the catDelitoDTO tag and printing catDelitoId and nombre.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<c:set var="xmlString">
    <listaCatalogo>
       <catDelitoDTO>
            <catDelitoId>1</catDelitoId>
            <claveDelito>1</claveDelito>
            <nombre>Violación</nombre>
            <esGrave>true</esGrave>
        </catDelitoDTO>
        <catDelitoDTO>
            <catDelitoId>3</catDelitoId>
            <claveDelito>3</claveDelito>
            <nombre>Daño en propiedad ajena</nombre>
            <esGrave>false</esGrave>
        </catDelitoDTO>
     </listaCatalogo>
</c:set>

<x:parse xml="${xmlString}" var="xmlOutput"/>

<x:forEach select="$xmlOutput//catDelitoDTO" var="dto">
    <x:out select="$dto//catDelitoId"/>
    <x:out select="$dto//nombre"/>
</x:forEach>

output:

1 Violación 3 Daño en propiedad ajena 

Solution

In your case simply assign the xmlString from the session scoped attribute delitosXML and then use the same code to iterate all the catDelitoDTO tags.

<c:set var="xmlString" value="${sessionScope.delitosXML }">

<x:parse xml="${xmlString}" var="xmlOutput"/>

<x:forEach select="$xmlOutput//catDelitoDTO" var="dto">
    <x:out select="$dto//catDelitoId"/>
    <x:out select="$dto//nombre"/>
</x:forEach>
Braj
  • 46,415
  • 5
  • 60
  • 76
-1

Try this:

<script>
    var delitos = $('<%=(String)request.getSession().getAttribute("delitosXML")%>');

     delitos.find('catDelitoDTO').each(function(){
        alert($(this)[0].innerHTML);
     });
</script>
AVK
  • 645
  • 9
  • 22
  • The OP actually needs to convert the source of the pseudo-XML as it is processed in jsp. – Jay Blanchard May 23 '14 at 17:33
  • Thanks AVK, the problem here is that this value i am getting it dynamically from the Session, and i can not do it like your example, at least i don't know how in this case. – Deutsche Mexa May 23 '14 at 17:36
  • @JayBlanchard How can i do that? – Deutsche Mexa May 23 '14 at 17:37
  • Modified my answer. Take a look. – AVK May 23 '14 at 17:37
  • @AVK I will try it like that, and then i tell you. – Deutsche Mexa May 23 '14 at 17:39
  • @AVK i get this in the console: SyntaxError: unterminated string literal, i will keep trying another solutions, thanks in advance. – Deutsche Mexa May 23 '14 at 17:56
  • In this case it means that your content probably has a single quote character that messes up with the javascript, Try using double quotes. Example, var delitos = $("<%= ... %>"); or something similar – AVK May 23 '14 at 18:01
  • It is the same problem with double quotes, maybe i can change the format in the Java file, so i can get it in one line, in this way the string it's not gonna be broken. – Deutsche Mexa May 23 '14 at 18:09
  • I doubt that a single line content is the problem. See if any special characters (like ñ, for example) is the issue here. Maybe apply a URI encoding to the content, before you send in to the client (browser). Like [this one](http://stackoverflow.com/questions/1265282/recommended-method-for-escaping-html-in-java) – AVK May 23 '14 at 18:12
  • Btw, firstly you could just try simulate passing some content, which doesnt contain any non-ascii characters, to jQuery. Like '1' and see if that works for you. If that works, then it's probably the non-ascii characters that are causing the problem. – AVK May 23 '14 at 18:27