xml parsing and text replacement

R

Robert Mark Bram

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
:)
 
M

Martin Honnen

Robert said:
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
 
E

Evertjan.

Martin Honnen wrote on 29 feb 2004 in
microsoft.public.inetserver.asp.general:
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.
 
R

Robert Mark Bram

Thank you Martin and Evertjan - your advice was very helpful!

Rob
:)

Martin Honnen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top