views:

362

answers:

1

hi guys, i'm totally new to jquery and i was looking around to see if this can be done. basically i have a xml tree structure that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<RecentTutorials>
  <Tutorial author="The Reddest">
    <Title>Silverlight and the Netflix API</Title>
    <Categories>
      <Category>Tutorials</Category>
      <Category>Silverlight 2.0</Category>
      <Category>Silverlight</Category>
      <Category>C#</Category>
      <Category>XAML</Category>
    </Categories>
    <Date>1/13/2009</Date>
  </Tutorial>
  <Mockup author="The Hairiest">
    <Title>Cake PHP 4 - Saving and Validating Data</Title>
    <Categories>
      <Category>Tutorials</Category>
      <Category>CakePHP</Category>
      <Category>PHP</Category>
    </Categories>
    <Date>1/12/2009</Date>
  </Mockup>
</RecentTutorials>

using jquery alone, can i select out the whole mockup node/tree with all the rest of it's children? i try playing with .text() but wasn't able to get anything. and my googling didn't lead me anywhere as well. hopefully you guys don't mind the dumb question :)

var dom3 = parseXML(str_xml);
var strTemp = $(dom3).find("Mockup");

so that the final output will be like so.

<Mockup author="The Hairiest">
        <Title>Cake PHP 4 - Saving and Validating Data</Title>
        <Categories>
          <Category>Tutorials</Category>
          <Category>CakePHP</Category>
          <Category>PHP</Category>
        </Categories>
        <Date>1/12/2009</Date>
      </Mockup>

thanks!

+1  A: 

what you had should work, except that you dont need to run parseXML(), just give jquery the xml string directly:

var strTemp = $(str_xml).find("Mockup");

i dont think there is a way to get the raw string out, but here is a hack if you want the string: you need to wrap the produced dom with a tag (any, as long as it does not duplicate "Mockup"):

var xmlString = $('<wrapper>').append(strTemp ).html();//strTemp  from above

produces

firebug_prompt> $('<wrapper>').append($('<root><ch>test</ch><ch>test2</ch></root>')).html()
<root><ch>test</ch><ch>test2</ch></root>
Chii
mmm, it seems that doing so will just pull out a object Object in the strTemp :( any ideas how to fix this?
melaos
what exactly are you doing? you should edit your question with new content, saying what you did and how it failed - otherwise its hard to tell what you did from a one line comment
Chii