473,320 Members | 1,859 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,320 software developers and data experts.

Generic Function for differing XML structure

code green
1,726 Expert 1GB
I have written code that accepts orders then writes them to XML.
I can create two formats of XML, one is bespoke to our CRM, the other is opXML under BOSS standards.

My final createXML function is entirely different for each format because the structure and element names differ.
Here are short extracts to demonstrate
Expand|Select|Wrap|Line Numbers
  1. -------opXML--------
  2. <InvoiceLine>
  3.     <LineNumber>1</LineNumber> 
  4.     <InvoiceLineReferences /> 
  5.     <Product>
  6.         <SuppliersProductCode>XX-XX-XX</SuppliersProductCode> 
  7.         <Description>nnnnnnnnnnnnnnnnn</Description> 
  8.     </Product>
  9.     <Quantity>
  10.         <Amount>2</Amount> 
  11.     </Quantity>
  12.     <Price>
  13.        <UnitPrice>35.5000</UnitPrice> 
  14.        <LineTotalNet>71.00</LineTotalNet> 
  15.        <LineTotalTax>12.43</LineTotalTax> 
  16.        <LineTotal>83.43</LineTotal> 
  17.     </Price>
  18.     <LineTax /> 
  19. </InvoiceLine>
Expand|Select|Wrap|Line Numbers
  1. ------Bespoke---------
  2. <OrderLine>
  3.     <LineNumber>1</LineNumber> 
  4.     <Product>XXX-XXX</Product> 
  5.     <Quantity>1</Quantity> 
  6.     <Price>7.7900</Price> 
  7.     <Personalised /> 
  8.     <DiscValue /> 
  9.     <LineTotalGross>7.79</LineTotalGross> 
  10.     <LineTotalNet /> 
  11.     <LineTotalTax /> 
  12. </OrderLine>
As you can see, as well as different elements names the hierarchy is also different.

I would like a generic function that could write either format depending on the function parameters.
Passing this associtive array seems a good start
Expand|Select|Wrap|Line Numbers
  1. array(element name1=>value,
  2. element name2=>value,
  3. element name3=>value) 
But I cannot think of a way to pass the structure/hierarchy to match the elements.
Any tips gratefully considered.
Jan 29 '10 #1
7 2146
dgreenhouse
250 Expert 100+
I've been toying with this concept for awhile myself. I haven't really spent the time to solve it yet, but I'll be working on something similar soon to emit valid xhtml fragments.

It appears that there are never attributes on elements - only values. Is this always the case?

Am I to assume that the difference in XML structure emitted is based on different fields drawn from queries? Is the acceptance of orders FROM a database or an external source? If from an external source – what's the input structure? Or are the orders from a front-end applicaton?

Just curious ~ what are you using to generate the XML? XMLWriter, DOM, "Roll your own?"

Also I'm curious about <InvoiceLineReferences />... I assume this is part of the opXML (BOSS) standard (never worked with it), but I'm just wondering what that means in the standard? Is this like a self-join relationship between line items?

Nice discussion start...!
Jan 29 '10 #2
Dormilich
8,658 Expert Mod 8TB
if you have an appropriately nested array, you could serialize that into a WDDX file and then transform that via XSLT into the desired XML format.
Jan 30 '10 #3
code green
1,726 Expert 1GB
dgreenhouse I've been toying with this concept for awhile myself
So I am not alone
It appears that there are never attributes on elements - only values. Is this always the case?
Elements can have attributes AND values, or one of each or neither - just serving as a heading or so it seems.
Is the acceptance of orders FROM a database or an external source? If from an external source – what's the input structure? Or are the orders from a front-end applicaton
The original order format may be CSV (including the awful ebay format), XLS or Web-page. I manipulate them all into MySQL databases, and also download orders from a remote MySQL connection, then into the bespoke XML
The opXML format originates from a MsSQL database so your asumption about different queries is correct.
I am using DOM objects and write using these functions
Expand|Select|Wrap|Line Numbers
  1. //Returns a DOM object element level
  2. function createEntity(&$xml,&$thisLevel,$tag)
  3. {
  4.     if(!empty($tag))
  5.     {
  6.            $newtextnode = $xml->createTextNode(chr(10));
  7.            $thisLevel->appendChild($newtextnode);
  8.  
  9.         $nextLevel = $xml->createElement($tag);
  10.         $thisLevel->appendChild($nextLevel);
  11.         return $nextLevel;
  12.     }
  13.     return false;
  14. }
  15.  
  16. //Writes values into passed DOM object element level
  17. function writeText(&$xml,&$level,$text)
  18. {
  19.     $value = $xml->createTextNode(mb_convert_encoding
  20.         (htmlspecialchars($text),'UTF-8'));
  21.        $level->appendChild($value);
  22. }
I have no function for writing attributes yet..
<InvoiceLineReferences />... contains these elements in BOSS
Expand|Select|Wrap|Line Numbers
  1. <OrderLineNumber>1</OrderLineNumber> 
  2.   <BuyersOrderLineReference>00001</BuyersOrderLineReference> 
  3.  
Feb 1 '10 #4
dgreenhouse
250 Expert 100+
code green,

Sorry I haven't responded yet...

I'm really interested in this subject and will ruminate on it over the next couple of days...

Best...
Feb 1 '10 #5
code green
1,726 Expert 1GB
Dormilich if you have an appropriately nested array, you could serialize that
Considered this, but to appropriately nest the array for each structure along with the differing elements would require a seperate function for each format. This is simply moving the structure creation to an earlier instance. Yes, I then can have a generic XMLcreate() but now have different structureCreate() functions for each format.

What do you think about two arrays.
One with key=>value, another with key=>parent.
I am already creating this array dynamically
Expand|Select|Wrap|Line Numbers
  1. //Array for element name and values
  2. array(elementName1=>value1 , 
  3. elementName2=>value2, 
  4. elementName3=>value3) 
by array merging of some default values and a query result
Expand|Select|Wrap|Line Numbers
  1. SELECT value1 AS elementName1, 
  2. value2 AS elementName2, 
  3. value3 AS elementName3
What about a hard coded array controlling the XML structure
Expand|Select|Wrap|Line Numbers
  1. //Array for element name and level
  2. array(elementName1=>parentNameX, 
  3. elementName2=>parentNameXX, 
  4. elementName3=>parentNameXX)
Not sure how to fit any required attributes in there unfortunately
Feb 1 '10 #6
kovik
1,044 Expert 1GB
The only way to do this that comes to mind for me is to have one function that, depending on a boolean parameter, sends the information to the appropriate function for XML formatting. This way, you could manually define the nesting structure for either method.
Feb 1 '10 #7
code green
1,726 Expert 1GB
Or......I could store an XML template of each format, then prior to processing the orders, parse the required template into an array, which would give me the structure and element names.
Then fill that array with values from my elementName=>value array
by matching the element names.

...but I am not sure how to parse the structure from XML to the same array format
Feb 1 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
5
by: Bob Weiner | last post by:
I'm new to C#. What is the best (in terms of programmability and use) data structure to use to maintain an array of information with containing 3 fields of differing types. For instance:...
6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
2
by: Dave Thorens | last post by:
Hi. My VB6 application has several collections of custom classes, and each class has differing properties (ie. the classes are not necessarily related). I want to write a generic function which can...
3
by: snesbit | last post by:
I have a structure called SearchAreaListItem. The structure has some properties. The application implements this as a collection.generic.list(of SearchAreaListItem) I load the collection up ...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
1
by: =?Utf-8?B?QkpT?= | last post by:
I have written a Generic method that takes an object, a type, T, and returns a System.Nullable (Of T) depending on whether or not the object IsDBNull. It was working fine until I tried to pass a...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.