convert javascript string to xml string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepthi230
    New Member
    • Feb 2008
    • 8

    convert javascript string to xml string

    how can i convert a javascript string to xml string?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    [code=javascript]var XMLobj = (new DOMParser()).pa rseFromString(t heString, "text/xml")[/code]

    Comment

    • deepthi230
      New Member
      • Feb 2008
      • 8

      #3
      Originally posted by acoder
      [code=javascript]var XMLobj = (new DOMParser()).pa rseFromString(t heString, "text/xml")[/code]
      the problem that i am facing is the string that is to be passed to the parseFromString method is a xmlstring. i have an xml file stored in my local file system. but,when i read the file using javascript it is assigned to a javascript string n i m getting a parseerror when i try parse it using parseFromString method.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Have you checked that the XML file is valid?

        Comment

        • deepthi230
          New Member
          • Feb 2008
          • 8

          #5
          i am storing a string obtained by converting a document fragment using the method serializeToStri ng in the xml file. The problem that i could figure out was the root element of the document fragment is not converted (only the child nodes are converted to string).So the resulting xml doc is not having a root element. How can i fix this??

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Can you post the code that you've tried.

            Comment

            • deepthi230
              New Member
              • Feb 2008
              • 8

              #7
              [CODE=javascript]var serializer =Components.cla sses["@mozilla.o rg/xmlextras/xmlserializer;1 "].createInstance (Components.int erfaces.nsIDOMS erializer);
              var string = serializer.seri alizeToString(t his.rootnode);

              var out = Components.clas ses["@mozilla.o rg/network/file-output-stream;1"].createInstance (Components.int erfaces.nsIFile OutputStream);
              out.init(this.f ile, 0x20 | 0x02, 0666, null);
              out.write(strin g,string.length );
              out.flush();
              out.close();[/CODE]
              Last edited by acoder; Mar 3 '08, 02:05 PM. Reason: Added code tags

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                See parsing and serializing XML.

                Comment

                Working...