473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1528
craigslist...@gmail.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\tAddress 1\n"
D+="\t\tAddress 2\n"
D+="\t\tAddress 3\n"
D+="\t\t\tHousehold\n"
D+="\t\t\t\tJohn\n"
D+="\t\t\t\tLucy\n"
D+="\tLos Angeles\n"
D+="\t\tAddress 1\n"
D+="\t\tAddress 2\n"
D+="\t\tAddress 3\n"
D+="\t\t\tHousehold\n"
D+="\t\t\t\tMike\n"
D+="\t\t\t\tRyan\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
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*...
16
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...
4
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
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...
3
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...
11
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...
9
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...
6
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...
3
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.