Projects‎ > ‎

Parser XML

This project defines how XML syntax will be parsed.  The parser org.openptk.structure.XmlConverter is bi-directional. It can "decode" an XML String into a hierarchy of OpenPTK Structures. It can also "encode" a hierarchy of OpenPTK Structures into a XML String.
  • Nested XML elements are added as Children to a Parent OpenPTK Structure
  • XML elements are identified as multi-value will have their nested elements added as Values to a Parent OpenPTK Structure.  Multi-valued XML elements are declared in the opentpk.xml configuration file.
This project uses the Java XML Stream XMLStreamReader and XMLStreamWriter 

Release 2.2
Singular Attribute

The value is a primitive type
  • String
  • Integer
  • Double
  • Boolean
  • NULL
<name type="...">value</name>

XML Structure
<string>string</string>

OpenPTK Structure
The structure will contain a single value which is a String:

StructureIF struct = new BasicStructure("name","value");


Complex Attribute ... a.k.a. sub-attributes
The value is a collection of key / value pairs
<name>
   <name1>value1</name1>
   <name2>value2</name2>
</name>


XML Structure
string : object (containing key/value pairs)

OpenPTK Structure
The structure will contain two sub-structures.  The sub-structures are added as "children"

StructureIF struct = new BasicStructure("name");
struct.addChild(new BasicStructure("name1","value1"));
struct.addChild(new BasicStructure("name2","value2"));


Multi-valued Attribute (primitives)

Each value is the same primitive type
  • String
  • Integer
  • Double
  • Boolean
  • NULL
<name type="...">
   <value>value1</value>
   <value>value2</value>
</name>

XML Structure
string : array (containing strings)

OpenPTK Structure
The structure will contain multiple children, there will be a child for each array object:

Examples:

StructureIF struct = new BasicStructure("roles");
struct.addValue("employee");
struct.addValue("manager");

<roles type="string">
   <role>employee</role>
   <role>manager</role>
</roles>


StructureIF struct = new BasicStructure("forgottenPasswordQuestions");
struct.addValue("City Born");
struct.addValue("Mothers Maiden");
struct.addValue("Frequent Flyer Number");

<forgottenPasswordQuestions type="string">
   <question>City Born</question>
   <question>Mothers Maiden</question>
   <question>Frequent Flyer Number</question>
</forgottenPasswordQuestions>



Multi-valued Attribute (structures)
Each "value" is a collection of objects (key-value pairs)
<names>
   <name>
      <name1>value1</name1>
      <name2>value2</name2>
      <name3>value3</name3>
   </name>
   <name>
      <name1>value1</name1>
      <name2>value2</name2>
   </name>
</names>

XML Structure
string : array (containing objects (level1), each level1 object contains key/value pairs)

OpenPTK Structure

StructureIF struct = new BasicStructure("name");

StructureIF item1 = new BasicStructure();
item1.addChild(new BasicStructure("name1","value1");
item1.addChild(new BasicStructure("name2","value2");
item1.addChild(new BasicStructure("name3","value3");

StructureIF item2 = new BasicStructure();
item2.addChild(new BasicStructure("name1","value1");
item2.addChild(new BasicStructure("name2","value2");

struct.addValue(item1);
struct.addValue(item2);

Implementation

The attribute "categories" (identified above) need to implemented using the OpenPTK StructureIF interface.

org.openptk.XMLConverter

 Category  Value XML Syntax  Pseudo Code
Singular
 primitive type<name>...</name> struct = new Structure("name","value")
Complex
(sub attributes)
 object<name>
   <name1>...</name1>
   <name2>...</name2>
</name>
struct = new Structure("name")
struct.addChild(new Structure("...","..."))
struct.addChild(new Structure("...","..."))
Multi-valued
 primitive type<name>
   <value>...</value>
   <value>...</value>
</name>
struct = new Structure("name")
struct.addValue("...")
struct.addValue("...")
Multi-valued
 object<names>
   <name>
      <name1>...</name1>
      <name2>...</name2>
   </name>
   <name>
      <name1>...</name1>
      <name2>...</name2>
   </name>
</names>
struct = new Structure("name")

val1 = new Structure()
val1.addChild(new Structure("","")
val1.addChild(new Structure("","")

val2 = new Structure()
val2.addChild(new Structure("","")
val2.addChild(new Structure("","")

struct.addValue(val1)
struct.addValue(val1)

Configuration

The following sample Converter configuration data indicates which XML elements support multi-values are its value element names are defined.

      <Converter type="xml" classname="org.openptk.structure.XmlConverter">
         <Structures>
            <Structure id="subjects">
               <Properties>
                  <Property name="multivalue" value="subject"/>
               </Properties>
            </Structure>
            <Structure id="forgottenPasswordAnswers">
               <Properties>
                  <Property name="multivalue" value="answer"/>
               </Properties>
            </Structure>
            <Structure id="forgottenPasswordQuestions">
               <Properties>
                  <Property name="multivalue" value="question"/>
               </Properties>
            </Structure>
            <Structure id="roles">
               <Properties>
                  <Property name="multivalue" value="role"/>
               </Properties>
            </Structure>
         </Structures>
      </Converter>

Example

This is a String representation, toString(), of a Structure hierarchy

subject={
   uniqueid="cuser";
   attributes={
      empno=123456;
      active=true;
      lastname="User";
      forgottenPasswordAnswers=[];
      title="Jersey (JSR-311) Expert";
      forgottenPasswordQuestions=[
         "Mothers Maiden Name",
         "City you were born",
         "Last 4 digits of Frequent Flyer"
      ];
      firstname="Curl";
      telephone="123-456-7890";
      email="restful@openptk.org";
      manager="owner";
      roles=["user","admin","operator"];
      fullname="Curl User";
      organization="openptk";
      manager=42L
   }
}

Here is the encode XML output

<subject>
   <uniqueid type="string">cuser</uniqueid>
   <attributes>
      <empno type="integer">123456</empno>
      <active type="boolean">true</active>
      <lastname type="string">User</lastname>
      <forgottenPasswordAnswers type="string"></forgottenPasswordAnswers>
      <title type="string">Jersey (JSR-311) Expert</title>
      <forgottenPasswordQuestions type="string">
         <question>Mothers Maiden Name</question>
         <question>City you were born</question>
         <question>Last 4 digits of Frequent Flyer</question>
      </forgottenPasswordQuestions>
      <firstname type="string">Curl</firstname>
      <telephone type="string">123-456-7890</telephone>
      <email type="string">restful@openptk.org</email>
      <manager type="string">owner</manager>
      <roles type="string">
         <role>user</role>
         <role>admin</role>
         <role>operator</role>
      </roles>
      <fullname type="string">Curl User</fullname>
      <organization type="string">openptk</organization>
      <manager type="long">42</manager>
   </attributes>
</subject>