Creating an XML file from an Object

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating an XML file from an Object

Julian Ford
Hello.

I have seen a few posts about parsing XML.  I need to be able to export an object as XML.
I have seen the JrcOrder example, but I am not able to get it to work quite right.
I believe I should get something that looks like this...

<Order number="O00001" status="Open">
    <Customer>
        CustomerID name="Joe Akarski" number="C13579"/>
        <Address>
            <Street>10 Main Street</Street>
            <City>Anytown</City>
            <State>AnyState</State>
            <PostalCode>12345</PostalCode>
            <Country>USA</Country>
        </Address>
        <Contact>Joe</Contact>
        <Phone>555-1212</Phone>
    </Customer>
    <OrderDate>2000-02-14</OrderDate>
    <LineItems>
        <LineItem number="1">
            <Quantity>12</Quantity>
            <Item number= "I12345" >
                <Description>Roses</Description>
                <UnitPrice>$3.50</UnitPrice>
            </Item>
        </LineItem>
        <LineItem number="2">
            <Quantity>1</Quantity>
            <Item number="I67890">
                <Description>Box of Candy</Description>
                <UnitPrice>$5.50</UnitPrice>
            </Item>
        </LineItem>
        <LineItem number="3">
            <Quantity>1</Quantity>
            <Item number="I4567">
                <Description>Valentine''s Day Card</Description>
                <UnitPrice>$2.99</UnitPrice>
            </Item>
        </LineItem>
    </LineItems>
</Order>

But instead, I am getting something that looks like this...

<Order number="O00001" status="Open" OrderDate="2000-02-14">
    <Customer Phone="555-1212" Contact="Joe">
        <CustomerID number="C13579" name="Joe Akarski" />
        <Address Street="10 Main Street" State="AnyState" Country="USA" PostalCode="12345" City="Anytown" />
    </Customer>
    <LineItems>
        <LineItem number="1" Quantity="12">
            <Item number="I12345" Description="Roses" UnitPrice="$3.50" />
        </LineItem>
        <LineItem number="2" Quantity="1">
            <Item number="I67890" Description="Box of Candy" UnitPrice="$5.50" />
        </LineItem>
        <LineItem number="3" Quantity="1">
            <Item number="I4567" Description="Valentine's Day Card" UnitPrice="$2.99" />
        </LineItem>
    </LineItems>
</Order>

It appears that I am not able to correctly export a field as an element...they keep showing up as
attributes.  For example, the instance variables of the Address are showing as attributes,
not elements as I had wanted.

I am using the mapping spec from the example (copied below), and then using
AbtXmlOutputSerializer new abtXmlPrint: anOrder on: aStream mappingSpecification: mappingSpec.

I have tried changing the <Attribute> tags to <SubElement>, but that actually makes it worse,
and all the attributes then simply show up as text nodes.

I am sure I am missing something very basic.
Any help that is offered would be greatly appreciated....!!!!!

Thanks in advance,
Julian Ford


The mapping spec:

<?xml version="1.0"?>
<!DOCTYPE XmlMappingSpec SYSTEM "abtxmap.dtd" >
<!-- Generated by VisualAge Smalltalk goodie on 2009-06-09-11.25.13.765000 -->
<XmlMappingSpec Name="order.map" NameSpaceURI="order">

    <!-- Mapping for element ''Address'' -->
    <ClassTypeMapping TypeName="Address" ClassName="JrcAddress">
        <AttributeMapping ClassAttribute="street">
            <Attribute>Street</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="state">
            <Attribute>State</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="country">
            <Attribute>Country</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="postalCode">
            <Attribute>PostalCode</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="city">
            <Attribute>City</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Customer'' -->
    <ClassTypeMapping TypeName="Customer" ClassName="JrcCustomer">
        <AttributeMapping ClassAttribute="phone">
            <Attribute>Phone</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="contact">
            <Attribute>Contact</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="customerID">
            <SubElement>CustomerID</SubElement>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="address">
            <SubElement>Address</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''CustomerID'' -->
    <ClassTypeMapping TypeName="CustomerID" ClassName="JrcCustomerID">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="name">
            <Attribute>name</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Item'' -->
    <ClassTypeMapping TypeName="Item" ClassName="JrcItem">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="description">
            <Attribute>Description</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="unitPrice">
            <Attribute>UnitPrice</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''LineItem'' -->
    <ClassTypeMapping TypeName="LineItem" ClassName="JrcLineItem">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="quantity">
            <Attribute>Quantity</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="item">
            <SubElement>Item</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''LineItems'' -->
    <ClassTypeMapping TypeName="LineItems" ClassName="JrcLineItems">
        <AttributeMapping ClassAttribute="lineItem">
            <SubElement>LineItem</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Order'' -->
    <ClassTypeMapping TypeName="Order" ClassName="JrcOrder">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="status">
            <Attribute>status</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="orderDate">
            <Attribute>OrderDate</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="customer">
            <SubElement>Customer</SubElement>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="lineItems">
            <SubElement>LineItems</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>
</XmlMappingSpec>

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/ANDzKcuWyMMJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Creating an XML file from an Object

jtuchel
Julian,

I completely understand your frustration, I've been in the same boat several times. Things do improve a lot once you add a schema to the game. There is a number of things that mapping specs alone simply don't support without a schema.

The fact that the use of an xml schema can make things better is good and bad news.
The good news is that you can make things more flexible without code changes.
The bad news is that mapping between xml and objects tends to become very complex even for simple tasks.

But still there are things that simply do not work (or at least I couldn't get to work). One of them is mapping two instance variables of an object to an attribute of a tag and its content like

Customer instance variables: type, custNo
<Customer type="longterm">123456</Customer>

Good luck,

Joachim



Am Montag, 5. November 2012 22:48:21 UTC+1 schrieb Julian Ford:
Hello.

I have seen a few posts about parsing XML.  I need to be able to export an object as XML.
I have seen the JrcOrder example, but I am not able to get it to work quite right.
I believe I should get something that looks like this...

<Order number="O00001" status="Open">
    <Customer>
        CustomerID name="Joe Akarski" number="C13579"/>
        <Address>
            <Street>10 Main Street</Street>
            <City>Anytown</City>
            <State>AnyState</State>
            <PostalCode>12345</PostalCode>
            <Country>USA</Country>
        </Address>
        <Contact>Joe</Contact>
        <Phone>555-1212</Phone>
    </Customer>
    <OrderDate>2000-02-14</OrderDate>
    <LineItems>
        <LineItem number="1">
            <Quantity>12</Quantity>
            <Item number= "I12345" >
                <Description>Roses</Description>
                <UnitPrice>$3.50</UnitPrice>
            </Item>
        </LineItem>
        <LineItem number="2">
            <Quantity>1</Quantity>
            <Item number="I67890">
                <Description>Box of Candy</Description>
                <UnitPrice>$5.50</UnitPrice>
            </Item>
        </LineItem>
        <LineItem number="3">
            <Quantity>1</Quantity>
            <Item number="I4567">
                <Description>Valentine''s Day Card</Description>
                <UnitPrice>$2.99</UnitPrice>
            </Item>
        </LineItem>
    </LineItems>
</Order>

But instead, I am getting something that looks like this...

<Order number="O00001" status="Open" OrderDate="2000-02-14">
    <Customer Phone="555-1212" Contact="Joe">
        <CustomerID number="C13579" name="Joe Akarski" />
        <Address Street="10 Main Street" State="AnyState" Country="USA" PostalCode="12345" City="Anytown" />
    </Customer>
    <LineItems>
        <LineItem number="1" Quantity="12">
            <Item number="I12345" Description="Roses" UnitPrice="$3.50" />
        </LineItem>
        <LineItem number="2" Quantity="1">
            <Item number="I67890" Description="Box of Candy" UnitPrice="$5.50" />
        </LineItem>
        <LineItem number="3" Quantity="1">
            <Item number="I4567" Description="Valentine's Day Card" UnitPrice="$2.99" />
        </LineItem>
    </LineItems>
</Order>

It appears that I am not able to correctly export a field as an element...they keep showing up as
attributes.  For example, the instance variables of the Address are showing as attributes,
not elements as I had wanted.

I am using the mapping spec from the example (copied below), and then using
AbtXmlOutputSerializer new abtXmlPrint: anOrder on: aStream mappingSpecification: mappingSpec.

I have tried changing the <Attribute> tags to <SubElement>, but that actually makes it worse,
and all the attributes then simply show up as text nodes.

I am sure I am missing something very basic.
Any help that is offered would be greatly appreciated....!!!!!

Thanks in advance,
Julian Ford


The mapping spec:

<?xml version="1.0"?>
<!DOCTYPE XmlMappingSpec SYSTEM "abtxmap.dtd" >
<!-- Generated by VisualAge Smalltalk goodie on 2009-06-09-11.25.13.765000 -->
<XmlMappingSpec Name="order.map" NameSpaceURI="order">

    <!-- Mapping for element ''Address'' -->
    <ClassTypeMapping TypeName="Address" ClassName="JrcAddress">
        <AttributeMapping ClassAttribute="street">
            <Attribute>Street</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="state">
            <Attribute>State</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="country">
            <Attribute>Country</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="postalCode">
            <Attribute>PostalCode</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="city">
            <Attribute>City</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Customer'' -->
    <ClassTypeMapping TypeName="Customer" ClassName="JrcCustomer">
        <AttributeMapping ClassAttribute="phone">
            <Attribute>Phone</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="contact">
            <Attribute>Contact</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="customerID">
            <SubElement>CustomerID</SubElement>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="address">
            <SubElement>Address</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''CustomerID'' -->
    <ClassTypeMapping TypeName="CustomerID" ClassName="JrcCustomerID">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="name">
            <Attribute>name</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Item'' -->
    <ClassTypeMapping TypeName="Item" ClassName="JrcItem">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="description">
            <Attribute>Description</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="unitPrice">
            <Attribute>UnitPrice</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''LineItem'' -->
    <ClassTypeMapping TypeName="LineItem" ClassName="JrcLineItem">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="quantity">
            <Attribute>Quantity</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="item">
            <SubElement>Item</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''LineItems'' -->
    <ClassTypeMapping TypeName="LineItems" ClassName="JrcLineItems">
        <AttributeMapping ClassAttribute="lineItem">
            <SubElement>LineItem</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Order'' -->
    <ClassTypeMapping TypeName="Order" ClassName="JrcOrder">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="status">
            <Attribute>status</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="orderDate">
            <Attribute>OrderDate</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="customer">
            <SubElement>Customer</SubElement>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="lineItems">
            <SubElement>LineItems</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>
</XmlMappingSpec>

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/ijxsHsw4flQJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Creating an XML file from an Object

Julian Ford
Thanks for the reply, Joachim.

Yes...it is somewhat frsutrating.  I had expected this to be a very simple thing.
I still think I am probably missing something fundamental, though, and that
eventually, there will be a "lightbulb" moment, and all will be clear.

I do see that schemas play an integral part in this framwork, and that is probably
as it should be, since they are the standard in XML definition.  And I understand
that the mapping spec relates these attributes/elements/etc with the Smalltalk
objects and their instance variables.
I just do not currently see how to use them all togetther....perhaps with a simple
API like...
#printXmlOf: anObject on: aStream using: aSchema withMapping: aMappingSpec.

Anyway...thanks for the note.

Regards,
Julian

On Tuesday, 6 November 2012 04:06:42 UTC-5, [hidden email] wrote:
Julian,

I completely understand your frustration, I've been in the same boat several times. Things do improve a lot once you add a schema to the game. There is a number of things that mapping specs alone simply don't support without a schema.

The fact that the use of an xml schema can make things better is good and bad news.
The good news is that you can make things more flexible without code changes.
The bad news is that mapping between xml and objects tends to become very complex even for simple tasks.

But still there are things that simply do not work (or at least I couldn't get to work). One of them is mapping two instance variables of an object to an attribute of a tag and its content like

Customer instance variables: type, custNo
<Customer type="longterm">123456</Customer>

Good luck,

Joachim



Am Montag, 5. November 2012 22:48:21 UTC+1 schrieb Julian Ford:
Hello.

I have seen a few posts about parsing XML.  I need to be able to export an object as XML.
I have seen the JrcOrder example, but I am not able to get it to work quite right.
I believe I should get something that looks like this...

<Order number="O00001" status="Open">
    <Customer>
        CustomerID name="Joe Akarski" number="C13579"/>
        <Address>
            <Street>10 Main Street</Street>
            <City>Anytown</City>
            <State>AnyState</State>
            <PostalCode>12345</PostalCode>
            <Country>USA</Country>
        </Address>
        <Contact>Joe</Contact>
        <Phone>555-1212</Phone>
    </Customer>
    <OrderDate>2000-02-14</OrderDate>
    <LineItems>
        <LineItem number="1">
            <Quantity>12</Quantity>
            <Item number= "I12345" >
                <Description>Roses</Description>
                <UnitPrice>$3.50</UnitPrice>
            </Item>
        </LineItem>
        <LineItem number="2">
            <Quantity>1</Quantity>
            <Item number="I67890">
                <Description>Box of Candy</Description>
                <UnitPrice>$5.50</UnitPrice>
            </Item>
        </LineItem>
        <LineItem number="3">
            <Quantity>1</Quantity>
            <Item number="I4567">
                <Description>Valentine''s Day Card</Description>
                <UnitPrice>$2.99</UnitPrice>
            </Item>
        </LineItem>
    </LineItems>
</Order>

But instead, I am getting something that looks like this...

<Order number="O00001" status="Open" OrderDate="2000-02-14">
    <Customer Phone="555-1212" Contact="Joe">
        <CustomerID number="C13579" name="Joe Akarski" />
        <Address Street="10 Main Street" State="AnyState" Country="USA" PostalCode="12345" City="Anytown" />
    </Customer>
    <LineItems>
        <LineItem number="1" Quantity="12">
            <Item number="I12345" Description="Roses" UnitPrice="$3.50" />
        </LineItem>
        <LineItem number="2" Quantity="1">
            <Item number="I67890" Description="Box of Candy" UnitPrice="$5.50" />
        </LineItem>
        <LineItem number="3" Quantity="1">
            <Item number="I4567" Description="Valentine's Day Card" UnitPrice="$2.99" />
        </LineItem>
    </LineItems>
</Order>

It appears that I am not able to correctly export a field as an element...they keep showing up as
attributes.  For example, the instance variables of the Address are showing as attributes,
not elements as I had wanted.

I am using the mapping spec from the example (copied below), and then using
AbtXmlOutputSerializer new abtXmlPrint: anOrder on: aStream mappingSpecification: mappingSpec.

I have tried changing the <Attribute> tags to <SubElement>, but that actually makes it worse,
and all the attributes then simply show up as text nodes.

I am sure I am missing something very basic.
Any help that is offered would be greatly appreciated....!!!!!

Thanks in advance,
Julian Ford


The mapping spec:

<?xml version="1.0"?>
<!DOCTYPE XmlMappingSpec SYSTEM "abtxmap.dtd" >
<!-- Generated by VisualAge Smalltalk goodie on 2009-06-09-11.25.13.765000 -->
<XmlMappingSpec Name="order.map" NameSpaceURI="order">

    <!-- Mapping for element ''Address'' -->
    <ClassTypeMapping TypeName="Address" ClassName="JrcAddress">
        <AttributeMapping ClassAttribute="street">
            <Attribute>Street</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="state">
            <Attribute>State</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="country">
            <Attribute>Country</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="postalCode">
            <Attribute>PostalCode</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="city">
            <Attribute>City</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Customer'' -->
    <ClassTypeMapping TypeName="Customer" ClassName="JrcCustomer">
        <AttributeMapping ClassAttribute="phone">
            <Attribute>Phone</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="contact">
            <Attribute>Contact</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="customerID">
            <SubElement>CustomerID</SubElement>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="address">
            <SubElement>Address</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''CustomerID'' -->
    <ClassTypeMapping TypeName="CustomerID" ClassName="JrcCustomerID">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="name">
            <Attribute>name</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Item'' -->
    <ClassTypeMapping TypeName="Item" ClassName="JrcItem">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="description">
            <Attribute>Description</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="unitPrice">
            <Attribute>UnitPrice</Attribute>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''LineItem'' -->
    <ClassTypeMapping TypeName="LineItem" ClassName="JrcLineItem">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="quantity">
            <Attribute>Quantity</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="item">
            <SubElement>Item</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''LineItems'' -->
    <ClassTypeMapping TypeName="LineItems" ClassName="JrcLineItems">
        <AttributeMapping ClassAttribute="lineItem">
            <SubElement>LineItem</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>

    <!-- Mapping for element ''Order'' -->
    <ClassTypeMapping TypeName="Order" ClassName="JrcOrder">
        <AttributeMapping ClassAttribute="number">
            <Attribute>number</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="status">
            <Attribute>status</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="orderDate">
            <Attribute>OrderDate</Attribute>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="customer">
            <SubElement>Customer</SubElement>
        </AttributeMapping>
        <AttributeMapping ClassAttribute="lineItems">
            <SubElement>LineItems</SubElement>
        </AttributeMapping>
    </ClassTypeMapping>
</XmlMappingSpec>

--
You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/va-smalltalk/-/iFQwbpLaZl8J.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/va-smalltalk?hl=en.