473,786 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing hierarchical tabbed string data into XML

Basically, I want to do this in javascript:

I want to convert a string such as:
Cities
New York
Address 1
Address 2
Address 3
Household
John
Lucy

Los Angeles
Address 1
Address 2
Address 3
Household
Mike
Ryan

....and automatically convert this string to XML so I can traverse
through it with any XML methods that may exist.

Is there a quick and easy way to do this?

Thanks!

Jun 11 '07 #1
1 1551
craigslist...@g mail.com wrote:
I want to convert a string such as:

Cities
New York
Address 1
Address 2
Address 3
Household
John
Lucy

Los Angeles
Address 1
Address 2
Address 3
Household
Mike
Ryan

...and automatically convert this string to XML so I can
traverse through it with any XML methods that may exist.
// data init
var D = "Cities\n"
D+="\tNew York\n"
D+="\t\tAddres s 1\n"
D+="\t\tAddres s 2\n"
D+="\t\tAddres s 3\n"
D+="\t\t\tHouse hold\n"
D+="\t\t\t\tJoh n\n"
D+="\t\t\t\tLuc y\n"
D+="\tLos Angeles\n"
D+="\t\tAddres s 1\n"
D+="\t\tAddres s 2\n"
D+="\t\tAddres s 3\n"
D+="\t\t\tHouse hold\n"
D+="\t\t\t\tMik e\n"
D+="\t\t\t\tRya n\n"

// valid xml knows 5 character entities only
D=D.replace(/</g, '&lt;')
D=D.replace(/>/g, '&gt;')
D=D.replace(/"/g, '&quot;')
D=D.replace(/'/g, '&apos;')
D=D.replace(/&/g, '&amp;')

// regexes based on tabs
D=D.replace(/(\t{4})(.{0,})\ n/g,' <name>$2</name>\n')
D=D.replace(/(\t{3})(.{0,})\ n/g,' <household>$2 </household>\n')
D=D.replace(/(\t{2})(.{0,})\ n/g,' <address>$2</address>\n')
D=D.replace(/(\t)(.{0,})\n/g, ' <city>$2</city>\n')

// add root tag and xml header line
var lines = D.split('\n')
var root = lines[0]
delete lines[0]
D='<'+root+'>' + lines.join('\n' ) + '</'+root+'>'
D='<?xml version="1.0" encoding="UTF-8"?>\n' + D

// report to screen
document.write( '<textarea rows="20" cols="70">'+D+' </textarea>')

Hope this helps,

--
Bart

Jun 12 '07 #2

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

Similar topics

4
3828
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply* access config files. This is version 3, which is a big overhaul. It extends ConfigObj to reading config files with sections and various other simplifications. I find ConfigObj extremely easy to use and use it for reading config files and data...
16
2907
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
4
2610
by: Daisy | last post by:
Let's say I've got a forum, where users can be moderators of each forum. Tables look like this: USER -------- user_key name FORUM
5
2257
by: clintonG | last post by:
I'm looking for documentation and would not turn my nose up to any code from anybody who thinks they are good at the design of an algorythm that can be used to generated a hierarchical relational data model. What? A Yahoo-like drill-down menu that is a series of categories and nested categories is a hierarchical relational data model. An example can be seen at but the review of the query string values strongly indicates
3
2774
by: kev | last post by:
Hello, I posted a question a while ago on tabbed pages, how to set it to invisible when the text box is empty.It was answered by Rick and the code ran perfectly. However, i tried using the same code for another scenario and it gives me compile error:method or data member not found. My scenario is i have a nested tab page.My main tabbed page has 4 tabs, About, SafetyLevel1,SafetyLevel2,SafetyLevel3. Inside this main, i created a...
11
2622
by: AndyM | last post by:
Hi, I have a curious problem that is causing me large amounts of grief and is steadily turning me grey. Hopefully you guys can help. I have a Master table that contains a CustomerID (as well as customer name etc), this is used as the Primary key to all the tables relating to that customer. I've been using 1 to Many relationships for most of the tables (e.g A customer can have many addresses) with Referential Integrity and cascading turned...
9
1766
by: Jasper | last post by:
Hi, I have multiple data files which need parsing in realtime so high performance is *crucial*. I dont have a format definition, but from what I can see there is a hierarchy of data. Each data field is named thus <"name":(the <are mine). The data can be quoted text or unquoted text or a composite hierarcy field. Each name/data pair is terminated by a comma unless it is the last in the
6
2394
by: Jasper | last post by:
Hi, Maybe this is off-topic, but perhaps you can help. I'm looking for ideas on how to parse a data file. I dont know XML but I know it parses data in text format. I have a structured data file of the general form shown below. I dont have any definition of the data. Basically it looks like it is hierarchical, token/data pairs defined by brackets and square brackets. I would like to parse this out into some sort of data object(s) in...
3
2825
by: Kailash Nadh | last post by:
Hello all. I have this tab formatted hierarchical structure. -------------------- 0 A 1 B 2 C 2 D 1 E --------------------
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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...
1
10110
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
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6748
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.