Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting the attributes and the text #16

Open
learninghuman opened this issue Jan 7, 2016 · 1 comment
Open

Converting the attributes and the text #16

learninghuman opened this issue Jan 7, 2016 · 1 comment

Comments

@learninghuman
Copy link

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.
@GeethanadhP
Copy link

well you will have to loop through the attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants