Skip to content

Converting the attributes and the text #16

@learninghuman

Description

@learninghuman

I have a need to convert XML to Avro and i am exploring this tool to achieve the same.

Xml Sample

<employee id="100">
    <department id="1000">HR</department>
</employee>

XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="department">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:short" name="id"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute type="xs:byte" name="id"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

Code for the conversion

public static void main(String[] args) {
        Schema schema = Converter.createSchema(new File("employee.xsd"));
        GenericData.Record record  = Converter.createDatum(schema, new File("employee.xml"));
        System.out.println(record.get("department"));
        System.out.println(record.get("employee"));

}

Output

{"id": 1000}

null

Questions

  1. How do i get the id attribute from the employee tag?
  2. How do i get the text from the department tag? The output prints only the attriabutes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions