how to create a GUI using data from xml file? - java

what i want is to create a dynamic GUI that consists of a set of buttons ( in my case each button represents an element from the xml file ) and when i click a button there will be a creation of buttons which represents its childs..i dnt know if it is possible to do that using DOM or JAXB..i'll be so thankful if anyone can help me or has another idea to do that!
thanks!!
<?xml version="1.0" encoding="UTF-8" ?>
- <HWData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="hw_data.xsd">
<Header time="2015-02-03T02:02:56" uploaded="false" version="1.0" />
- <NE MOID="NE-SGSN-387826" objectClass="NE" objectClassVersion="1" NEId="387826" NEType="SGSN" systemTitle="SGSN-MNB-" locationName="MANOUBA" systemReleaseVersion="J8" softwareReleaseVersion="J8 50.70-0 50" serialNumber="387826" vendorName="Nokia">
- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11" equipmentHolderType="Cabinet" equipmentHolderSpecificType="EC217_A" locationName="1A" serialNumber="" interchangeability="" version="" identificationCode="" vendorName="Nokia">
- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11100" equipmentHolderType="Cartridge" equipmentHolderSpecificType="ACH16_A" locationName="1A001-00" serialNumber="XY131403380" interchangeability="" version="" identificationCode="" vendorName="Nokia">
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-1-PIU-1" objectClass="UNIT" objectClassVersion="1" unitId="1" unitTypeActual="ACPI4_A" unitTypeExpected="ACPI4_A" position="1" operationalState="enabled" serialNumber="FK124600007" interchangeability="B" version="3C" identificationCode="C111969" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-1-RTM-1" objectClass="UNIT" objectClassVersion="1" unitId="1" unitTypeActual="CPRT4_A" unitTypeExpected="CPRT4_A" position="1" operationalState="enabled" serialNumber="JG130400410" interchangeability="A" version="2A" identificationCode="C111970" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-FAN-2" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400960" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0/UNIT-FAN-1" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400980" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
</EQHO>
<EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-1-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="111000501" equipmentHolderType="Cartridge" equipmentHolderSpecificType="CHAF2_A" locationName="1A001-00-FFI-1" serialNumber="31401904" interchangeability="A" version="" identificationCode="C110639" vendorName="Nokia" />
- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11200" equipmentHolderType="Cartridge" equipmentHolderSpecificType="ACH16_A" locationName="1A002-00" serialNumber="XY131403370" interchangeability="" version="" identificationCode="" vendorName="Nokia">
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-1-PIU-1" objectClass="UNIT" objectClassVersion="1" unitId="1" unitTypeActual="ACPI4_A" unitTypeExpected="ACPI4_A" position="1" operationalState="enabled" serialNumber="FK124600059" interchangeability="B" version="3C" identificationCode="C111969" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-SHM-2" objectClass="UNIT" objectClassVersion="1" unitId="3" unitTypeActual="ASMGR_A" unitTypeExpected="ASMGR_A" position="3" operationalState="enabled" serialNumber="XY131402619" interchangeability="A" version="4B" identificationCode="C110581" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-FAN-3" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400977" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-FAN-1" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400971" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0/UNIT-FAN-2" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400975" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
</EQHO>
<EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-2-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="112000502" equipmentHolderType="Cartridge" equipmentHolderSpecificType="CHAF2_A" locationName="1A002-00-FFI-2" serialNumber="31401893" interchangeability="A" version="" identificationCode="C110639" vendorName="Nokia" />
- <EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="11300" equipmentHolderType="Cartridge" equipmentHolderSpecificType="ACH16_A" locationName="1A003-00" serialNumber="XY131403377" interchangeability="" version="" identificationCode="" vendorName="Nokia">
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-SHM-1" objectClass="UNIT" objectClassVersion="1" unitId="3" unitTypeActual="ASMGR_A" unitTypeExpected="ASMGR_A" position="3" operationalState="enabled" serialNumber="XY131301773" interchangeability="A" version="4B" identificationCode="C110581" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-FAN-1" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400963" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-FAN-2" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400962" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
<UNIT MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0/UNIT-FAN-3" objectClass="UNIT" objectClassVersion="1" unitId="4" unitTypeActual="AFAMO_A" unitTypeExpected="AFAMO_A" position="4" operationalState="enabled" serialNumber="XY131400988" interchangeability="B" version="3B" identificationCode="C110638" vendorName="Nokia" />
</EQHO>
<EQHO MOID="NE-SGSN-387826/DN:EQHO-1A/EQHO-3-0" objectClass="EQHO" objectClassVersion="1" equipmentHolderId="113000503" equipmentHolderType="Cartridge" equipmentHolderSpecificType="CHAF2_A" locationName="1A003-00-FFI-3" serialNumber="31401903" interchangeability="A" version="" identificationCode="C110639" vendorName="Nokia" />
</EQHO>
</NE>
</HWData>

Removed previous answer based on XStream in favor of JAXB.
Add the jaxb2-maven-plugin to your pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/xsd/</schemaDirectory>
</configuration>
</plugin>
Put your XSD in src/main/resources/xsd/
The class HwDataTree extends DynamicTreeDemo which is part of an Oracle tutorial. Download both classes and add them to your project.
import generated.EQHO;
import generated.HWData;
import generated.NE;
import generated.UNIT;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.List;
public class HwDataTree extends DynamicTreeDemo {
// the path of your XML file
private static final String PATH = "C:/example.xml";
#Override
public void populateTree(DynamicTree treePanel) {
HWData hwData = initData();
treePanel.addObject(null, formatHeader(hwData));
treePanel.addObject(null, formatNE(hwData));
addNodesRecursive(hwData.getNE(), null, treePanel);
}
private HWData initData() {
try {
File xml = new File(PATH);
JAXBContext jaxbContext = JAXBContext.newInstance(HWData.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (HWData) jaxbUnmarshaller.unmarshal(xml);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void addNodesRecursive(NE parent, DefaultMutableTreeNode parentNode, DynamicTree treePanel) {
List<EQHO> children = parent.getEQHO();
for (EQHO child : children) {
DefaultMutableTreeNode childNode = treePanel.addObject(parentNode, formatEQHO(child));
addNodesRecursive(child, childNode, treePanel);
}
}
private void addNodesRecursive(EQHO parent, DefaultMutableTreeNode parentNode, DynamicTree treePanel) {
List<Object> children = parent.getEQHOAndUNIT();
for (Object child : children) {
DefaultMutableTreeNode childNode = treePanel.addObject(parentNode, getMOID(child));
if (child instanceof EQHO) {
EQHO eqho = (EQHO) child;
addNodesRecursive(eqho, childNode, treePanel);
}
}
}
private String getMOID(Object child) {
String moid;
if (child instanceof EQHO) {
EQHO eqho = (EQHO) child;
moid = formatEQHO(eqho);
} else if (child instanceof UNIT) {
UNIT unit = (UNIT) child;
moid = formatUnit(unit);
} else {
throw new RuntimeException("Could not parse type of: " + child);
}
return moid;
}
private String formatHeader(HWData hwData) {
return String.format("Header time=%s", hwData.getHeader().getTime());
}
private String formatNE(HWData hwData) {
return String.format("NE MOID=%s", hwData.getNE().getMOID());
}
private String formatEQHO(EQHO child) {
return String.format("EQHO MOID=%s", child.getMOID());
}
private String formatUnit(UNIT unit) {
return String.format("UNIT MOID=%s", unit.getMOID());
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Tree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HwDataTree newContentPane = new HwDataTree();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
}
Generate the wrappers by executing mvn clean compile.
Then run HwDataTree, the result looks like this:

this is my xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v5 rel. 4 U (http://www.xmlspy.com) by XMLSPY 5 Professional Ed. Release 4, Installed Multi + SMP for 2 users (Nokia GmbH) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.20">
<!-- Version history:
1.20: - changed "xsd" namespace according to W3C official recommendation
- removed Telcordia specific pattern from EquipmentHolderSpecificTypes
- added AvailabilityStatusEntry value "inCharge"
- made "time" attribute in <Header/> mandatory
- added serviceState attribute to PRUT
- made PRUT attributes optional
- added mnemonic attribute to EQHO
- allow empty strings for attributes "position", "manufacturingDate", "dateOfLastService"
- SoftwareReleaseVersionType: 20 -> 30
1.12: vendorName: mandatory, non-empty
1.11: PositionType: string -> integer
EquipmentHolderSpecificTypes: pattern refined according to Telcordia Network Engineer Java extensions
Additional attributes in <Header/>
time: timeInstant -> dateTime
1.10: Length of FunctionalUnitTypes: 4 -> 10
Length of SoftwareReleaseVersionType: 10->20
Length of VersionType: 5->30
-->
<xsd:simpleType name="AdministrativeState">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="locked"/>
<xsd:enumeration value="unlocked"/>
<xsd:enumeration value="shuttingDown"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ConnectorTypes">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:simpleType name="DateOrEmptyType">
<xsd:union memberTypes="xsd:date EmptyStringType"/>
</xsd:simpleType>
<!-- whitespaces are allowed -->
<xsd:simpleType name="EmptyStringType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\s*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="EquipmentHolderTypes">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
<xsd:enumeration value="Cabinet"/>
<xsd:enumeration value="Rack"/>
<xsd:enumeration value="Subrack"/>
<xsd:enumeration value="Cartridge"/>
<xsd:enumeration value="Slot"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="EquipmentHolderSpecificTypes">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="FunctionalUnitIdType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="FunctionalUnitTypes">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="HWCheckSumType">
<xsd:restriction base="xsd:integer"/>
</xsd:simpleType>
<xsd:simpleType name="IdentificationCodeType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="InterchangeabilityType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="LocationNameType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="MOID">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="256"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="MnemonicType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NEAddressType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="256"/>
<xsd:pattern value="[-_.0-9a-zA-Z]+=.*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NamingAttributeIdType">
<xsd:restriction base="xsd:integer">
<xsd:maxInclusive value="999999999"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NameFromPlanningSystemType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NETypes">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="NumberOfSlotsOccupiedType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1"/>
<xsd:maxInclusive value="99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ObjectClassType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="5"/>
<xsd:pattern value="[A-Z]*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ObjectClassVersionType">
<xsd:restriction base="xsd:integer">
<xsd:maxInclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="OperationalState">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="disabled"/>
<xsd:enumeration value="enabled"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="PositionType">
<xsd:union memberTypes="xsd:integer EmptyStringType"/>
</xsd:simpleType>
<xsd:simpleType name="PowerSupplyTypes">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ProtectionGroupTypes">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="plus"/>
<xsd:enumeration value="colon"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="SerialNumberType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="35"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ServiceStateType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="active"/>
<xsd:enumeration value="standby"/>
<xsd:enumeration value="undefined"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="SoftwareReleaseVersionType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="SystemReleaseVersionType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="256"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="SystemTitleType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="256"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="UnitTypes">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="UserLabelType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="128"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="VendorNameType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="VersionType">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="AvailabilityStatus">
<xsd:sequence>
<xsd:element name="AvailabilityStatusEntry" minOccurs="0" maxOccurs="unbounded">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="inCharge"/>
<xsd:enumeration value="inTest"/>
<xsd:enumeration value="failed"/>
<xsd:enumeration value="powerOff"/>
<xsd:enumeration value="offLine"/>
<xsd:enumeration value="offDuty"/>
<xsd:enumeration value="dependency"/>
<xsd:enumeration value="degraded"/>
<xsd:enumeration value="notInstalled"/>
<xsd:enumeration value="logFull"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="GlobalConnectorId">
<xsd:attribute name="connectorHolderMOID" type="MOID"/>
<xsd:attribute name="connectorIdentifier" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="MOIDList">
<xsd:sequence>
<xsd:element name="MOID" type="MOID" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="PhysicalConnectorList">
<xsd:sequence>
<xsd:element name="Connector" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence minOccurs="0">
<xsd:element name="connectedToIdentifier" type="GlobalConnectorId"/>
</xsd:sequence>
<xsd:attribute name="connectorType" type="ConnectorTypes"/>
<xsd:attribute name="connectorIdentifier" type="xsd:string"/>
<xsd:attribute name="location" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:attributeGroup name="MOGeneralAttributes">
<xsd:attribute name="MOID" type="MOID" use="required"/>
<xsd:attribute name="objectClass" type="ObjectClassType"/>
<xsd:attribute name="objectClassVersion" type="ObjectClassVersionType"/>
</xsd:attributeGroup>
<xsd:attributeGroup name="MOStateAttributes">
<xsd:attribute name="administrativeState" type="AdministrativeState"/>
<xsd:attribute name="operationalState" type="OperationalState"/>
</xsd:attributeGroup>
<xsd:element name="availabilityStatus" type="AvailabilityStatus"/>
<xsd:element name="physicalConnectorList" type="PhysicalConnectorList"/>
<xsd:element name="UNIT">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="2">
<xsd:element ref="availabilityStatus" minOccurs="0"/>
<xsd:element ref="physicalConnectorList" minOccurs="0"/>
</xsd:sequence>
<xsd:attributeGroup ref="MOGeneralAttributes"/>
<xsd:attributeGroup ref="MOStateAttributes"/>
<xsd:attribute name="unitId" type="NamingAttributeIdType"/>
<xsd:attribute name="unitTypeActual" type="UnitTypes" use="required"/>
<xsd:attribute name="unitTypeExpected" type="UnitTypes"/>
<xsd:attribute name="position" type="PositionType"/>
<xsd:attribute name="numberOfSlotsOccupied" type="NumberOfSlotsOccupiedType"/>
<xsd:attribute name="userLabel" type="UserLabelType"/>
<xsd:attribute name="softwareReleaseVersion" type="SoftwareReleaseVersionType"/>
<xsd:attribute name="vendorName" type="VendorNameType" use="required"/>
<xsd:attribute name="version" type="VersionType"/>
<xsd:attribute name="serialNumber" type="SerialNumberType"/>
<xsd:attribute name="manufacturingDate" type="DateOrEmptyType"/>
<xsd:attribute name="dateOfLastService" type="DateOrEmptyType"/>
<xsd:attribute name="interchangeability" type="InterchangeabilityType"/>
<xsd:attribute name="identificationCode" type="IdentificationCodeType"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="PRUT">
<xsd:complexType>
<xsd:attributeGroup ref="MOGeneralAttributes"/>
<xsd:attribute name="unitPointer" type="MOID"/>
<xsd:attribute name="protectionUnitId" type="NamingAttributeIdType"/>
<xsd:attribute name="protecting" type="xsd:boolean"/>
<xsd:attribute name="priority" type="xsd:integer"/>
<xsd:attribute name="serviceState" type="ServiceStateType"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="EQHO">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="physicalConnectorList" type="PhysicalConnectorList" minOccurs="0"/>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="EQHO" minOccurs="0"/>
<xsd:element ref="UNIT" minOccurs="0"/>
</xsd:sequence>
</xsd:sequence>
<xsd:attributeGroup ref="MOGeneralAttributes"/>
<xsd:attributeGroup ref="MOStateAttributes"/>
<xsd:attribute name="equipmentHolderId" type="NamingAttributeIdType"/>
<xsd:attribute name="equipmentHolderSpecificType" type="EquipmentHolderSpecificTypes" use="required"/>
<xsd:attribute name="equipmentHolderType" type="EquipmentHolderTypes" use="required"/>
<xsd:attribute name="userLabel" type="UserLabelType"/>
<xsd:attribute name="vendorName" type="VendorNameType" use="required"/>
<xsd:attribute name="version" type="VersionType"/>
<xsd:attribute name="position" type="PositionType"/>
<xsd:attribute name="locationName" type="LocationNameType"/>
<xsd:attribute name="serialNumber" type="SerialNumberType"/>
<xsd:attribute name="manufacturingDate" type="DateOrEmptyType"/>
<xsd:attribute name="dateOfLastService" type="DateOrEmptyType"/>
<xsd:attribute name="interchangeability" type="InterchangeabilityType"/>
<xsd:attribute name="identificationCode" type="IdentificationCodeType"/>
<xsd:attribute name="mnemonic" type="MnemonicType"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="POSU">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="poweredEquipmentPtrList" type="MOIDList" minOccurs="0"/>
</xsd:sequence>
<xsd:attributeGroup ref="MOGeneralAttributes"/>
<xsd:attribute name="powerSupplyId" type="NamingAttributeIdType"/>
<xsd:attribute name="powerSupplyType" type="PowerSupplyTypes"/>
<xsd:attribute name="powerSource" type="xsd:integer"/>
<xsd:attribute name="operationalState" type="OperationalState"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="FUUT">
<xsd:complexType>
<xsd:sequence>
<xsd:sequence minOccurs="0" maxOccurs="2">
<xsd:element name="supportedByUnitList" type="MOIDList" minOccurs="0"/>
<xsd:element name="affectedObjectList" type="MOIDList" minOccurs="0"/>
</xsd:sequence>
<xsd:element ref="FUUT" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attributeGroup ref="MOGeneralAttributes"/>
<xsd:attributeGroup ref="MOStateAttributes"/>
<xsd:attribute name="functionalUnitId" type="FunctionalUnitIdType"/>
<xsd:attribute name="functionalUnitType" type="FunctionalUnitTypes" use="required"/>
<xsd:attribute name="userLabel" type="UserLabelType"/>
<xsd:attribute name="version" type="VersionType"/>
<xsd:attribute name="protected" type="xsd:boolean"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="PRGR">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="availabilityStatus" type="AvailabilityStatus" minOccurs="0"/>
<xsd:element ref="PRUT" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attributeGroup ref="MOGeneralAttributes"/>
<xsd:attribute name="protectionGroupId" type="NamingAttributeIdType"/>
<xsd:attribute name="protectionGroupType" type="ProtectionGroupTypes" use="required"/>
<xsd:attribute name="revertive" type="xsd:boolean"/>
<xsd:attribute name="operationalState" type="OperationalState"/>
<xsd:attribute name="waitToRestoreTime" type="xsd:integer" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="Header">
<xsd:complexType>
<xsd:attribute name="time" type="xsd:dateTime" use="required"/>
<xsd:attribute name="version" type="xsd:string"/>
<xsd:attribute name="uploaded" type="xsd:boolean"/>
<xsd:attribute name="GlobalId" type="xsd:integer"/>
<xsd:attribute name="AdaptationName" type="xsd:string"/>
<xsd:attribute name="AccessProtocol" type="xsd:string"/>
<xsd:attribute name="ProprietaryMediatorAddress" type="NEAddressType"/>
<xsd:attribute name="ProprietaryElementAddress" type="NEAddressType"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="NE">
<xsd:complexType>
<xsd:sequence minOccurs="0">
<xsd:element ref="EQHO" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="POSU" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="FUUT" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="PRGR" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attributeGroup ref="MOGeneralAttributes"/>
<xsd:attributeGroup ref="MOStateAttributes"/>
<xsd:attribute name="NEId" type="xsd:string" use="required"/>
<xsd:attribute name="NEType" type="NETypes" use="required"/>
<xsd:attribute name="systemTitle" type="SystemTitleType"/>
<xsd:attribute name="systemReleaseVersion" type="SystemReleaseVersionType"/>
<xsd:attribute name="serialNumber" type="SerialNumberType"/>
<xsd:attribute name="softwareReleaseVersion" type="SoftwareReleaseVersionType"/>
<xsd:attribute name="userLabel" type="UserLabelType"/>
<xsd:attribute name="vendorName" type="VendorNameType" use="required"/>
<xsd:attribute name="version" type="VersionType"/>
<xsd:attribute name="locationName" type="LocationNameType"/>
<xsd:attribute name="HWCheckSum" type="HWCheckSumType"/>
<xsd:attribute name="nameFromPlanningSystem" type="NameFromPlanningSystemType"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="HWData">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Header"/>
<xsd:element ref="NE"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Related

Compare XML using xmlunit when order of Attributes is different is not working

I have 2 xmls : Basically two XSD schemas
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="my_export_file">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row">
<xsd:complexType>
<xsd:attributeGroup ref="rowattr" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attributeGroup ref="docelattr" />
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="rowattr">
<xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
**<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**
<xsd:attribute name="src_system_id" type="Str.80" use="required" />
</xsd:attributeGroup>
<xsd:attributeGroup name="docelattr">
<xsd:attribute name="reporting_date" type="xsd:string" />
<xsd:attribute name="interface_type" type="xsd:string" />
</xsd:attributeGroup>
<xsd:simpleType name="Str.1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Str.80">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="80" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
and
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="my_export_file">
<xsd:complexType>
<xsd:sequence minOccurs="0" maxOccurs="unbounded">
<xsd:element name="row">
<xsd:complexType>
<xsd:attributeGroup ref="rowattr" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attributeGroup ref="docelattr" />
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="rowattr">
<xsd:attribute name="subject_level_ind" type="Str.1" use="optional" />
<xsd:attribute name="src_system_id" type="Str.80" use="required" />
**<xsd:attribute name="object_level_ind" type="Str.1" use="optional" />**
</xsd:attributeGroup>
<xsd:attributeGroup name="docelattr">
<xsd:attribute name="reporting_date" type="xsd:string" />
<xsd:attribute name="interface_type" type="xsd:string" />
</xsd:attributeGroup>
<xsd:simpleType name="Str.1">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="Str.80">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="80" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
I want to compare those and give differences. My code works fine but the only issue is if order of attributes is different it doesn't treat them as "SIMILAR". As you can see in the example, my xmls are same with just one change - order of object_level_ind is different. I want my code to not return this difference.
Code
var fis1 = new FileReader("C:\\Users\\test1.xsd ");
var fis2 = new FileReader("C:\\Users\\test2.xsd");
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(fis1,fis2));
diff.overrideElementQualifier(new ElementNameAndTextQualifier());
List<?> allDifferences = diff.getAllDifferences();
System.out.println(allDifferences);
I Also tried:
DifferenceEvaluator evaluator = DifferenceEvaluators
.downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_SEQUENCE);
Diff diff = DiffBuilder.compare(fis1)
.withTest(fis2).ignoreComments()
.ignoreWhitespace()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
.withDifferenceEvaluator(evaluator)
.checkForSimilar()
.build();
System.out.println("Differences: " + diff);
I Also tried solution given in comparing two xmls using xmlunit ignorng their order
But for my xml it gives:
identical: false
similar : false
Please let me know if any pointers.
Best Regards,
Abhi
This is happening because setIgnoteAttributeOrder ignores the order of the attributes of a node/element and not the actual order of node/element in the XML document. So, while <xsd:attribute name="object_level_ind" type="Str.1" use="optional" /> and <xsd:attribute use="optional" name="object_level_ind" type="Str.1" /> are considered same, the order of the elements is not ignored.
This answer here may have more details on how to compare ignoring element order - Compare two XML strings ignoring element order

How to create all possible XMLs?

How can I create all possible XMLs of these XSD:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://beep2000/client/beep.de" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://beep2000/client/beep.de">
<xsd:complexType name="TextType">
<xsd:sequence>
<xsd:element name="Text" minOccurs=1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="StreetType">
<xsd:sequence>
<xsd:element name="Street" type="xsd:string" minOccurs="1"/>
<xsd:element name="HouseNumber" type="xsd:int" minOccurs="1"/>
<xsd:element name="Suffix" type="xsd:string" minOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="1"/>
<xsd:enumeration value="A"/>
<xsd:enumeration value="B"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BodyType">
<xsd:sequence>
<xsd:element name="FirstPart" type="TextType" minOccurs="1"/>
<xsd:element name="SecondPart" type="TextType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AdressType">
<xsd:sequence>
<xsd:element name="Street" type="StreetType" minOccurs="0">
<xsd:element name="PostalCode" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="5"/>
<xsd:pattern value="[0-9]{5}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="City" type="xsd:string" minOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RequestEnvelope">
<xsd:sequence>
<xsd:element name="Title" type="xsd:string" minOccurs="0"/>
<xsd:element name="Forename" type="xsd:string" minOccurs="1"/>
<xsd:element name="Surname" type="xsd:string" minOccurs="0"/>
<xsd:element name="PersonalID" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="20"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Adress" type="AdressType" minOccurs="0"/>
<xsd:element name="Body" type="BodyType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ResponseEnvelope">
<xsd:sequence>
<xsd:element name="Result" minOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="3"/>
<xsd:enumeration value="OK"/>
<xsd:enumeration value="NOK"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Errorcode" type="xsd:integer" minOccurs="0"/>
<xsd:element name="Errortext" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Comment" type="TextType" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="MsgResponse" type="ResponseEnvelope"/>
<xsd:element name="MsgRequest" type="RequestEnvelope"/>
</xsd:schema>
There are some fields which are optional and some field which are mandatory. So there is a variety of XMLs. How can I find all possible structures of XMLs?
I expect something like:
XML No.1:
Field 1 --> mandatory
Field 2 --> mandatory
Field 3 --> optional
......
XML No.2.:
Field 1 --> mandatory
Field 2 --> mandatory
Field 4 --> mandatory
.....
Generating all possible XML files that conform to this schema would take an infinite amount of time and they would occupy an infinite amount of space.
However, you can generate a selection of sample documents using tools such as those described here:
How to generate sample XML documents from their DTD or XSD?

Namespace error in XSD

The error when validated using SAX parser is:
"org.xml.sax.SAXParseException; systemId: file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd; lineNumber: 10; columnNumber: 31; src-resolve.4.1: Error resolving component 'name'.
It was detected that 'name' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'.
If 'name' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'name' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'."
address.xml
<?xml version ="1.0" encoding="UTF-8"?>
<address
xmlns:personal="Personal things"
xmlns:houses="Regarding to houses"
xmlns="http://www.w3schools.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
xsd:schemaLocation="address.xsd"
>
<name>
<personal:title>Mr.</personal:title>
<first-name>Samitha</first-name>
<last-name>Chathuranga</last-name>
</name>
<sssd></sssd>
<house-id>
<houses:title>107 B</houses:title>
<NAME>Sam&apos;s Home</NAME>
<!-- An intnal entity is used for the single quote in House Name here-->
</house-id>
<village>Poramba</village>
<city district="Galle" province="Southern">AG</city>
<postal-code>80300</postal-code>
<country>Sri Lanka</country>
</address>
address.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/SampleSchema"
xmlns:tns="http://www.example.org/SampleSchema"
>
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="name" />
<xsd:element ref="house-id" />
<xsd:element ref="village" />
<xsd:element ref="city" />
<xsd:element ref="postal-code" />
<xsd:element ref="country" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="name">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="title" />
<xsd:element ref="first-name" />
<xsd:element ref="last-name" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="title" type="xsd:string" />
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
<xsd:element name="house-id">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="title" />
<xsd:element ref="NAME" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="NAME" type="xsd:string" />
<xsd:element name="village" type="xsd:string" />
<xsd:element name="country" type="xsd:string" />
<xsd:element name="city">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="2" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="postal-code">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
How to resolve this error?
Well your xsd is invalid. You are constructing it wrong. Here are my suggestions. Download XMLSpy or Liquid XML studio and then view my examples to see how to construct a proper XSD and then validate the XSD against it. These editors are a great way to visually see the XML documents and xsd's. They will help a lot.
Essentially you need to declare your types in the XSD then create elements based on those types. While your approach can work I would suggest that you study the approach I have taken it is pretty easy. I could explain it all but I think you will get the idea rather quickly.
Address.xsd
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com)-->
<xsd:schema xmlns:address="http://www.example.org/AddressSchema"
targetNamespace="http://www.example.org/AddressSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person_name"
type="address:person_name_type" />
<xsd:element name="ssd"
type="address:ssd_type" />
<xsd:element name="house_id"
type="address:house_id_type" />
<xsd:element name="village"
type="address:village_type" />
<xsd:element name="city"
type="address:city_type" />
<xsd:element name="postal_code"
type="address:postalcode_type" />
<xsd:element name="country"
type="address:country_type" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="postalcode_type">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="city_type">
<xsd:restriction base="xsd:string">
<xsd:length value="2" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="village_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="country_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="title_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="first_name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="last_name_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:complexType name="house_id_type">
<xsd:sequence>
<xsd:element name="title"
type="address:title_type" />
<xsd:element name="name"
type="address:name_type" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="person_name_type">
<xsd:sequence>
<xsd:element name="title"
type="address:title_type" />
<xsd:element name="first_name"
type="address:first_name_type" />
<xsd:element name="last_name"
type="address:last_name_type" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ssd_type">
<xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>
Address.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com) -->
<address xsi:schemaLocation="http://www.example.org/AddressSchema D:\GroundZero\address.xsd" xmlns="http://www.example.org/AddressSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<person_name>
<title>string</title>
<first_name>string</first_name>
<last_name>string</last_name>
</person_name>
<ssd>string</ssd>
<house_id>
<title>string</title>
<name>string</name>
</house_id>
<village>string</village>
<city>AB</city>
<postal_code>80300</postal_code>
<country>string</country>
</address>
I would also note that your instance document has the root (address) element in namespace http://www.w3schools.com, while your schema defines an address element in namespace http://www.example.org/AddressSchema. You can't just sprinkle namespaces around like fairy-dust and hope they add glamour to your code; they are fundamental and you need to get them right.

Swap XML elements using javax.xml.transform.Transformer

I have following xml portion
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
I have to swap the enumeration elements and attributeGroup elements as floows
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
. Can I do this using DTD file with javax.xml.transform.Transformer. How can I do this ?

javax.xml.transform.Transformer swaps my elements

I have following XSD section
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
</xsd:restriction>
I use org.apache.ws.commons.schema class to build it and after some time I retrieve it using javax.xml.transform.Transformer class. When I transform it following is the result
<xsd:complexType name="xyz">
<xsd:annotation>
<xsd:appinfo>
<Base ...../>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleContent>
<xsd:restriction base="niem-xsd:token">
<xsd:attributeGroup ref="s:SimpleObjectAttributeGroup"/>
<xsd:enumeration value="Consumer">
<xsd:annotation/>
</xsd:enumeration>
<xsd:enumeration value="abc">
<xsd:annotation/>
</xsd:enumeration>
</xsd:restriction>
</xsd:simpleContent>
The enumeration and attributeGroup elements are changed the position and that causes validation error. Can I avoid this interchange in transformation ? What is going on there ?

Categories