converting a segmented string into XML

alanchinese

Regular
Joined
Jan 12, 2005
Messages
62
Hi,

Is there a simple and efficient way to create an XML from a segmented string?
For example, I would like to convert the following string (is there a better name for this kind of strings?):

Code:
SEG_ROOT_START*1234~
SEG_LVL1*A*B~
SEG_LVL2*8~
SEG_LVL1*X*~
SEG_ROOT_END~
into:
Code:
<SEG_ROOT>
   <SEG_LVL1>
      <SEG_LVL1_01>A</SEG_LVL1_01>
      <SEG_LVL1_02>B</SEG_LVL1_02>
      <SEG_LVL2>
         <SEG_LVL2_01>8</SEG_LVL2_01>
      </SEG_LVL2>
   </SEG_LVL1>
   <SEG_LVL1>
      <SEG_LVL1_01>X</SEG_LVL1_01>
      <SEG_LVL1_01 />
   </SEG_LVL1>
</SEG_ROOT>

In this example,
  • The root segment has two forms, one for begin, one for end. We need to parse this specially
  • There are two levels, LVL1 (expecting two string childrens) and LVL2 (expecting one integer child)
  • Level LVL2 can only be children of LVL1
  • When an expected value shows an empty string, the corresponding XML node shouuld be NULL.

Currently, we are reading and parsing these strings manually. I am wondering if there are existing ways (regular expressions, maybe?) so that we can convert them into XMLs.

We can do the following with the XMLs:
  1. Data validation once XSD is defined.
  2. Data transformation once XSLT is defined.
  3. Data transmission via web services.

Thanks a lot for any information or suggestions,
Alan.
 
If you are processing them manually, as long as you are using the StringBuilder class you're probably not going to get much better performance than that.

There may be an elegent way to use regular expressions, but I'll have to defer to someone else.
 
I've had a good amount of experience with xml, and no, I'd say in this case, there's no parsing shortcut.
 
Back
Top