473,732 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML node containing markup

Hi folks,

I have an XML node called "myNode" and it contains:

"This is some text"

Now I can use the myNode.nodeValu e property to get the string of text
above. But say myNode contains:

"This is <em>some text</em>"

The property myNode.nodeValu e will now be "This is ". How can I get the
exact string above returned?

Thanks,

--
Dylan Parry - http://electricfreedom.org

A Flower?
Jul 13 '06 #1
8 1994


Dylan Parry wrote:

I have an XML node
What kind of node is that exactly, an element node, a text node, a
fragment node?

"This is <em>some text</em>"

How can I get the
exact string above returned?
It depends on the XML object model you use, with MSXML that you use in
ASP or in IE you can serialize each node using its property named 'xml' so
node.xml
gives you the serialized XML of the node.
However in the above case you have
This is <em>some text</em>
contained in some other node (probably an element node) and you need to
be aware that if you use
node.xml
on the container node that you get the start tag, attributes, and end
tag of the container node serialized as well.
Thus if you only want to serialize the child nodes then you need to loop
through and concatenate the xml of each child node (a text node, an
element node, a text node in that example).

With the W3C DOM to serialize a DOM node you would need DOM Level 3 Load
and Save, currently inside browsers only Opera 8 and 9 have a minimal
implementation of that.
However Mozilla introduced the proprietary XMLSerializer and other
browsers have implemented that too (Opera, recent Safari, Konqueror) so
there to serialize a node you do e.g.
var xmlSerializer = new XMLSerializer() ;
then to serialize a node to a string
var s = xmlSerializer.s erializeToStrin g(node);
Again for your case above you would need to loop over the child nodes of
the container node and serialize each child node.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 13 '06 #2
Martin Honnen wrote:
node.xml
[or]
var s = xmlSerializer.s erializeToStrin g(node);
Thanks. The above two, or rather a combination of the above, will do the
job nicely. I've set up a for loop to concatenate all of the nodes, but
obviously IE supports the former and not the latter, and everything else
seems to support the latter and not the former.

This is where I need to test for supported features! The trouble is that
I can't seem to figure out how to test for support of "node.xml" nor
"xmlSeriali zer" :( Any pointers?

--
Dylan Parry - http://webpageworkshop.co.uk

A Flower?
Jul 13 '06 #3
Dylan Parry wrote:
Hi folks,

I have an XML node called "myNode" and it contains:

"This is some text"

Now I can use the myNode.nodeValu e property to get the string of text
above. But say myNode contains:

"This is <em>some text</em>"

The property myNode.nodeValu e will now be "This is ". How can I get the
exact string above returned?
You can use textContent, and if not supported recurse down all the
childNodes collecting the text content of all the text nodes, something
like:

function getTextContent( el)
{
// Use textContent if supported
if (typeof el.textConent == 'string') return el.textContent;

// Otherwise, recurse down the childNodes & siblings
var cNode, cNodes = el.childNodes;
var txt = '';
for (var i=0, len=cNodes.leng th; i<len; ++i){
cNode = cNodes[i];
if (1 == cNode.nodeType) {
txt += getTextContent( cNode);
}
if (3 == cNode.nodeType) {
txt += cNode.data;
}
}
return txt;
}

--
Rob
Jul 13 '06 #4
if you do this:

<node><![CDATA[ Any kind of html including <b>tags</b]]></node>
then node.nodeValue will include the html.

Jul 13 '06 #5
go*****@gmail.c om wrote:
if you do this:

<node><![CDATA[ Any kind of html including <b>tags</b]]></node>

then node.nodeValue will include the html.
Hmm, that looks like a much better solution all round, to be honest!

--
Dylan Parry - http://electricfreedom.org

A Flower?
Jul 13 '06 #6


Dylan Parry wrote:
This is where I need to test for supported features! The trouble is that
I can't seem to figure out how to test for support of "node.xml" nor
"xmlSeriali zer" :( Any pointers?
if (typeof XMLSerializer != 'undefined')

See <http://www.faqts.com/knowledge_base/view.phtml/aid/34646/fid/616>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 13 '06 #7

Dylan Parry wrote:
go*****@gmail.c om wrote:
if you do this:

<node><![CDATA[ Any kind of html including <b>tags</b]]></node>

then node.nodeValue will include the html.

Hmm, that looks like a much better solution all round, to be honest!
Ah, so you wanted to *keep* the markup... :-x

--
Rob

Jul 13 '06 #8
RobG wrote:
Ah, so you wanted to *keep* the markup... :-x
Yes :) It's a bit of a cludge, but I found that suddenly I needed an XML
node to contain an anchor element that I really didn't want to have to
deal with when I parse the file. I suppose it's just me being lazy for
the time being, but with working to a tight deadline I don't have time
(yet) to rewrite too much code. Saying that, I do have "REWRITE ENTIRE
JS LIBRARY" as a big heading, so it *will* happen at some point ;)

--
Dylan Parry - http://electricfreedom.org

A Flower?
Jul 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3125
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are the defined contents of the TABLE element. The TR element is not defined as an "immediate" or "direct" contained element of TABLE. Given that
3
1208
by: chris yoker via DotNetMonster.com | last post by:
hi, I have an xmlFile <code> <rows> <row> <PRODUCT-TYPE>bike</PRODUCT-TYPE> <PRODUCT-DATE>01/01/2004</PRODUCT-DATE> <ADDED-NODE>"blah"<ADDED-NODE /> </row>
2
3153
by: Kael | last post by:
Hi, I’ve made a class to help me accomplish some things faster but I’m having problems inserting nodes. I’ve tried many different ways, but with no success. This is a very stripped down version of what I’m trying to do. I know that I’m simply missing some basic understanding, so hopefully someone can enlighten me. Public Class MYXMLCLASS Public theXMLDocument As New XmlDataDocument Public myXPath As String
6
2746
by: SHC | last post by:
Hi all, I created an application from the Console Application (.NET) of VC++ .NET 2003, and I did "Build" the application of the attached .cpp file, volcanoes.xml and geology.dtd on my VC++ .NET 2003 - Windows XP Pro PC suscessfully. But when I ran it from the command line - C:\Documents and Settings\SHC\My Documents\Visual Studio Projects\XMLdtdValidatingReader\Debug>XMLdtdValidatingReader valcanoes.xml, I got the following message in...
14
2383
by: madtom1999 | last post by:
I'm trying to put a non-breaking space ( or any other entity for that matter) into a node eg: node.nodeValue+='&nbsp'; however the actual code is written in the html -ie it displays as >&nbsp;< and not > < any ideas?
14
11503
by: neerajb | last post by:
Hi, I am having an XML document(input.xml) which is showing the menu heirarchy used in my application.My requirement is to add "submenu" tag to those menuitems who are having the child menuitems as shown in output.xml. I am using VB.NET framework 1.1. Please Help, i have already invested my 2 days but unable to build the logic as input xml may be having any level of nesting of menuitems.
1
6565
by: ahoway | last post by:
I am having problems deleting a node from a link list. I need to delete the node which contains the number six. This is what I have so far..... Thank you in advance. #include <iostream> #include "stdafx.h" using namespace std; class IntNode
10
8811
by: Simon Brooke | last post by:
The DOM API has included public Node importNode(Node,boolean) as a method of the Document interface for a long time. Does anything actually implement it? Xerces 2 is giving me: org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation. at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown Source) at org.apache.xerces.dom.CoreDocumentImpl.importNode(Unknown Source)
11
2322
by: =?Utf-8?B?TTFpUw==?= | last post by:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node (Grantee) like the following: <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group"> <URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI> </Grantee> However I am unclear on how to add these types of attributes. The following doesn't seem to work.
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.