RE: implementing a custom interface
"Jason Berk" <[email protected]>
| Newsgroups | gmane.text.xml.xmlbeans.user |
|---|---|
| Message-ID | <13B10822BFDBE64A8BE9937D5F91505B05CC27D6@pefcu-fs44.purdueefcu.com> |
I'm confused. Like I said, I can't change the way the XML looks when it
comes from the external system. So I can't use attributes....only
elements because that's how it comes to me.
my problem is this:
<foo>
<a>
<b>
<X>
<c>
</foo>
<bar>
<a>
<b>
<Y>
<c>
</bar
How do I factor <a> <b> and <c> into a "base" type and extend that base
type in <foo> and <bar>. Realize <X> and <Y> are in the middle and I
can't change that.
Jason
________________________________
From: Gillen, Paul [mailto:[email protected]]
Sent: Friday, September 18, 2009 2:34 PM
To: '[email protected]'
Subject: RE: implementing a custom interface
My colleagues say this wasn't clear.
The base type defined in the XSD in effect becomes the interface to the
implementing types. This approach keeps you painting between the lines
in the XSD world rather than drifting off the beam customizing XMLBeans.
The example below implements a complexType ProductType that has the two
attributes you want in your interface, beginning and ending balance.
The 3 complexTypes SavingsType, CheckingType, and CertificateType extend
ProductType in the XSD and therefore implement the interface in the
code. Note in the code example that the common information is available
either from the base type (ProductType) or the implementing type.
Paul Gillen
----------
XSD
----------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="xsd.account.bank.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="xsd.account.bank.com" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="BankExample">
<xs:complexType>
<xs:sequence>
<xs:element name="Accounts">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Savings"
type="SavingsType"/>
<xs:element name="Checking"
type="CheckingType"/>
<xs:element name="Certificate"
type="CertificateType"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="ProductType">
<xs:attribute name="startingBal" type="xs:float"
use="required"/>
<xs:attribute name="endingBal" type="xs:float"
use="required"/>
</xs:complexType>
<xs:complexType name="SavingsType">
<xs:complexContent>
<xs:extension base="ProductType">
<xs:attribute name="savingsStuff"
type="xs:string" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="CheckingType">
<xs:complexContent>
<xs:extension base="ProductType">
<xs:attribute name="checkingStuff"
type="xs:string" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="CertificateType">
<xs:complexContent>
<xs:extension base="ProductType">
<xs:attribute name="certificateStuff"
type="xs:string" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
----------
CODE
----------
package com.bank.account;
import java.io.File;
import org.apache.xmlbeans.QNameSet;
import org.apache.xmlbeans.XmlObject;
import com.bank.account.xsd.BankExampleDocument;
import com.bank.account.xsd.CertificateType;
import com.bank.account.xsd.CheckingType;
import com.bank.account.xsd.ProductType;
import com.bank.account.xsd.SavingsType;
import com.bank.account.xsd.BankExampleDocument.BankExample.Accounts;
import com.bank.account.xsd.BankExampleDocument.BankExample;
public class AccountProcessor
{
private final static String xmlFileName = "Account.xml";
public static void main(String[] args)
throws Exception
{
AccountProcessor module = new AccountProcessor();
module.go(args);
}
private void go(String[] args)
throws Exception
{
BankExampleDocument doc =
BankExampleDocument.Factory.parse(new File(xmlFileName));
String rootURI =
doc.schemaType().getDocumentElementName().getNamespaceURI();
BankExample bankExample = doc.getBankExample();
Accounts accounts =
bankExample.getAccounts();
XmlObject[] accountsChildren =
accounts.selectChildren(QNameSet.forWildcardNamespaceString("##any",
rootURI));
if (accountsChildren != null)
{
for (XmlObject accountChild:accountsChildren)
{
ProductType product = (ProductType) accountChild;
System.out.println("Info from ProductType:
"+product.getStartingBal() +" "+product.getEndingBal());
if (accountChild instanceof SavingsType)
{
SavingsType account = (SavingsType)
accountChild;
System.out.println("Savings
Type:\t\t"+account.getSavingsStuff());
System.out.println("Starting
Balance:\t"+account.getStartingBal());
System.out.println("Ending
Balance:\t\t"+account.getEndingBal());
}
else if (accountChild instanceof CheckingType)
{
CheckingType account = (CheckingType)
accountChild;
System.out.println("Checking
Type:\t\t"+account.getCheckingStuff());
System.out.println("Starting
Balance:\t"+account.getStartingBal());
System.out.println("Ending
Balance:\t\t"+account.getEndingBal());
}
else if (accountChild instanceof CertificateType)
{
CertificateType account = (CertificateType)
accountChild;
System.out.println("Certificate
Type:\t"+account.getCertificateStuff());
System.out.println("Starting
Balance:\t"+account.getStartingBal());
System.out.println("Ending
Balance:\t\t"+account.getEndingBal());
}
}
}
}
}
----------
SAMPLE XML
----------
<?xml version="1.0" encoding="UTF-8"?>
<BankExample xmlns="xsd.account.bank.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="xsd.account.bank.com xsd\BankExample.xsd">
<Accounts>
<Savings savingsStuff="Savings Info" endingBal="3.14"
startingBal="13.14"/>
<Checking checkingStuff="Checking Info" endingBal="23.14"
startingBal="33.14"/>
<Certificate certificateStuff="Certificate Info"
endingBal="43.14" startingBal="53.14"/>
</Accounts>
</BankExample>
----------
OUTPUT
----------
Info from ProductType: 13.14 3.14
Savings Type: Savings Info
Starting Balance: 13.14
Ending Balance: 3.14
Info from ProductType: 33.14 23.14
Checking Type: Checking Info
Starting Balance: 33.14
Ending Balance: 23.14
Info from ProductType: 53.14 43.14
Certificate Type: Certificate Info
Starting Balance: 53.14
Ending Balance: 43.14
-----Original Message-----
From: Gillen, Paul [mailto:[email protected]]
Sent: Friday, September 18, 2009 11:36 AM
To: '[email protected]'
Subject: RE: implementing a custom interface
If I understand you correctly, in your XSD create a base type with the
common attributes. Create types for each of your 3 derived from the
base type. In your code you can refer to each as basetype and for
specifics test with instanceof.
Paul Gillen
-----Original Message-----
From: Jason Berk [mailto:[email protected]]
Sent: Friday, September 18, 2009 11:19 AM
To: [email protected]
Subject: implementing a custom interface
I have three types in my XSD: Savings, Checking, Certificate.
Is it possible to add something (to the .xsdconfig?) that makes all
three types implement an interface I created?
I want my beans to be like this:
Product
getStaringBal()
getEndingBal()
Savings implements Product
getSavingsSpecificStuff()
Checking implements Product
getCheckingSpecificStuff()
make sense?
Jason
Now serving Boiler Spirit with every purchase. Switch to the new Purdue
debit card today. Show your pride and earn Scorecard Rewards on the
side. Just sign for your purchases and score valuable rewards points you
can use for travel, electronics or even cash.
***This is a transmission from Purdue Employees Federal Credit
Union (PEFCU) and is intended solely for its authorized
recipient(s), and may contain information that is confidential
and or legally privileged. If you are not an addressee, or the
employee or agent responsible for delivering it to an addressee,
you are hereby notified that any use, dissemination,
distribution, publication or copying of the information
contained
in this email is strictly prohibited. If you have received this
transmission in error, please notify us by telephoning (765)
497-3328 or returning the email. You are then instructed to
delete the information from your computer. Thank you for your
cooperation.***
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
Now serving Boiler Spirit with every purchase. Switch to the new Purdue debit card today. Show your pride and earn Scorecard Rewards on the side. Just sign for your purchases and score valuable rewards points you can use for travel, electronics or even cash.
***This is a transmission from Purdue Employees Federal Credit
Union (PEFCU) and is intended solely for its authorized
recipient(s), and may contain information that is confidential
and or legally privileged. If you are not an addressee, or the
employee or agent responsible for delivering it to an addressee,
you are hereby notified that any use, dissemination,
distribution, publication or copying of the information
contained
in this email is strictly prohibited. If you have received this
transmission in error, please notify us by telephoning (765)
497-3328 or returning the email. You are then instructed to
delete the information from your computer. Thank you for your
cooperation.***