Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > xml parsing and text replacement

Reply
Thread Tools

xml parsing and text replacement

 
 
Robert Mark Bram
Guest
Posts: n/a
 
      02-29-2004
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



 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-29-2004


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/

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      02-29-2004
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)
 
Reply With Quote
 
Robert Mark Bram
Guest
Posts: n/a
 
      03-01-2004
Thank you Martin and Evertjan - your advice was very helpful!

Rob


"Martin Honnen" <(E-Mail Removed)> wrote in message
news:uKcWyts$(E-Mail Removed)...
>
>
> 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/
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What libraries should I use for MIME parsing, XML parsing, and MySQL ? John Levine Ruby 0 02-02-2012 11:15 PM
SAX parsing problem, when element contains text like "[text]" Kai Schlamp Java 1 03-27-2008 08:36 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Print XML parsing to JspWriter (out) Class org.xml.sax.helpers.NewInstance can not access a member of class javax.xml.parsers.SAXParser with modifiers "protected" Per Magnus L?vold Java 0 11-15-2004 02:27 PM
Assistance parsing text file using Text::CSV_XS Domenico Discepola Perl Misc 6 09-02-2004 03:55 PM



Advertisments