Announcement Announcement Module
Collapse
No announcement yet.
how to access attributes from XML nodes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to access attributes from XML nodes

    I am trying to access the values of attributes within my XML nodes using a converter within the XStreamMarshaller. I am able to read the nodes and the values of those nodes but have been unable to access the attributes - they return a null value.

    this is the code from my application context:
    .............
    <bean id="xmlmarshaller" class="org.springframework.oxm.xstream.XStreamMars haller">
    <property name="aliases">
    <util:map id="aliases">
    <entry key="statement" value="com.batch.xmltask.XmlInputData" />
    </util:map>
    </property>
    <property name="converters" ref="headerConverter" />
    </bean>

    <!-- Converters -->
    <bean id="headerConverter" class="com.batch.converters.HeaderConverter" />
    .............

    the xml file that I am reading is as below:

    ...........
    <statement header="N0000|NEWCUSTOMER STATEMENT|">
    <accountNumber header="B1009">23</accountNumber>
    <customerName header="B1002">julius</customerName>
    <addressLine1 header="B1003"></addressLine1>
    <addressLine2 header="B1004"></addressLine2>
    <addressLine3 header="B1005"></addressLine3>
    <addressLine4 header="B1006"></addressLine4>
    <zip header="B1007" />
    <statementDate header="B1010" />
    </statement>
    ..........

    the attribute I am trying to read is 'header' and this is the code from my converter:

    public class HeaderConverter implements Converter {

    ..............@Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    XmlInputData inputData = new XmlInputData();
    System.out.println(reader.getAttribute("header"));
    System.out.println(reader.getNodeName());
    while (reader.hasMoreChildren()) {
    reader.moveDown();
    System.out.println(reader.getNodeName());
    System.out.println(reader.getAttribute("header"));
    reader.moveUp();
    }
    return inputData;
    }
    ...............
    }

    and this is the output after running a test case:

    4:53:46,851 INFO SimpleJobLauncher:109 - Job: [FlowJob: [name=simpleJob]] launched with the following parameters: [{}]
    14:53:46,881 INFO AbstractJob:347 - Executing step: [TaskletStep: [name=step1]]
    null
    statement
    accountNumber
    null
    customerName
    null
    addressLine1
    null
    addressLine2
    null
    addressLine3
    null
    addressLine4
    null
    zip
    null
    statementDate
    null
    .................

    thanks in advance!

  • #2
    resolveed

    I managed to resolve this by using reader.getAttribute(0) instead of reader.getAttribute("header") - seems like a bug in XStreamMarshaller

    Robin

    Comment

    Working...
    X