xml parsing and text replacement

Discussion in 'ASP General' started by Robert Mark Bram, Feb 29, 2004.

  1. Hi all!

    I want to use asp to do text find and replace with XHTML elements.. for
    example, let's say I have the following XHTML:

    <note>
    <from id="123">Jani</from>
    <to>Tove</to>
    <message>Norwegian: æøå. French: êèé</message>
    </note>

    I want to read this document in, find and XML element with id="123" and
    replace its text with "New author".

    Can I do this in ASP?

    Thanks for any advice!

    Rob
    :)
     
    Robert Mark Bram, Feb 29, 2004
    #1
    1. Advertising

  2. Robert Mark Bram wrote:

    > I want to use asp to do text find and replace with XHTML elements.. for
    > example, let's say I have the following XHTML:
    >
    > <note>
    > <from id="123">Jani</from>
    > <to>Tove</to>
    > <message>Norwegian: æøå. French: êèé</message>
    > </note>


    That is XML but not XHTML.

    > I want to read this document in, find and XML element with id="123" and
    > replace its text with "New author".
    >
    > Can I do this in ASP?


    Yes, using MSXML:

    <%@ Language="JScript" %>
    <%
    var xmlDocument = Server.CreateObject("Msxml2.DOMDocument.4.0");
    xmlDocument.async = false;
    var loaded = xmlDocument.load(Server.MapPath("test20040229.xml"));
    if (loaded) {
    xmlDocument.setProperty("SelectionLanguage", "XPath");
    var from = xmlDocument.selectSingleNode("/note/from[@id = '123']");
    if (from) {
    from.text = "New Author";
    }
    Response.ContentType = "text/xml";
    xmlDocument.save(Response);
    }
    else {
    Response.Write("<p>Error loading XML document: " +
    xmlDocument.parseError.reason + ".<\/p>");
    }
    %>

    The script uses MSXML 4 which usually needs to be installed, look on
    msdn.microsoft.com for the download.
    Or change the 4.0 to 3.0 and it should work on machines where IE6 is
    installed

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 29, 2004
    #2
    1. Advertising

  3. Robert Mark Bram

    Evertjan. Guest

    Martin Honnen wrote on 29 feb 2004 in
    microsoft.public.inetserver.asp.general:
    >> I want to read this document in, find and XML element with id="123" and
    >> replace its text with "New author".
    >>
    >> Can I do this in ASP?

    >
    > Yes, using MSXML:


    If it is only a ontime task, you could use VBS replace:

    txt = Replace(txt,
    "<from id=""123"">Jani</from>","<from id=""123"">New author</from>")

    Regex replace is also an option.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 29, 2004
    #3
  4. Thank you Martin and Evertjan - your advice was very helpful!

    Rob
    :)

    "Martin Honnen" <> wrote in message
    news:uKcWyts$...
    >
    >
    > Robert Mark Bram wrote:
    >
    > > I want to use asp to do text find and replace with XHTML elements.. for
    > > example, let's say I have the following XHTML:
    > >
    > > <note>
    > > <from id="123">Jani</from>
    > > <to>Tove</to>
    > > <message>Norwegian: æøå. French: êèé</message>
    > > </note>

    >
    > That is XML but not XHTML.
    >
    > > I want to read this document in, find and XML element with id="123" and
    > > replace its text with "New author".
    > >
    > > Can I do this in ASP?

    >
    > Yes, using MSXML:
    >
    > <%@ Language="JScript" %>
    > <%
    > var xmlDocument = Server.CreateObject("Msxml2.DOMDocument.4.0");
    > xmlDocument.async = false;
    > var loaded = xmlDocument.load(Server.MapPath("test20040229.xml"));
    > if (loaded) {
    > xmlDocument.setProperty("SelectionLanguage", "XPath");
    > var from = xmlDocument.selectSingleNode("/note/from[@id = '123']");
    > if (from) {
    > from.text = "New Author";
    > }
    > Response.ContentType = "text/xml";
    > xmlDocument.save(Response);
    > }
    > else {
    > Response.Write("<p>Error loading XML document: " +
    > xmlDocument.parseError.reason + ".<\/p>");
    > }
    > %>
    >
    > The script uses MSXML 4 which usually needs to be installed, look on
    > msdn.microsoft.com for the download.
    > Or change the 4.0 to 3.0 and it should work on machines where IE6 is
    > installed
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
    >
     
    Robert Mark Bram, Mar 1, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus

    Text replacement

    Markus, Dec 16, 2005, in forum: XML
    Replies:
    4
    Views:
    1,022
    Markus
    Dec 16, 2005
  2. Alexander Schmolck

    re.sub replacement text \-escapes woe

    Alexander Schmolck, Feb 13, 2004, in forum: Python
    Replies:
    4
    Views:
    387
    Alexander Schmolck
    Feb 14, 2004
  3. John Levine
    Replies:
    0
    Views:
    767
    John Levine
    Feb 2, 2012
  4. Erik Wasser
    Replies:
    5
    Views:
    533
    Peter J. Holzer
    Mar 5, 2006
  5. Phrogz
    Replies:
    1
    Views:
    123
    Martin Honnen
    Oct 31, 2006
Loading...

Share This Page