0

My transaction xml is shown below

<?xml version= "1.0"?>
<transactionlist>
<transaction action="c">
    <transactionid>t004</transactionid>
    <transactiondate>11/06/2013</transactiondate>
    <merchantdetails>Sony wholesale Dealer</merchantdetails>
    <itempurchased>3</itempurchased>
    <amount>40399</amount>
    <description>sony laptops</description>
</transaction>
<transaction action="d">
    <transactionid>t003</transactionid>
</transaction>
<transaction action="u">
    <transactionid>T001</transactionid>
    <transactiondate>20/08/2013</transactiondate>
    <merchantdetails>samsung Axses</merchantdetails>
    <itempurchased>1</itempurchased>
    <amount>40000</amount>
    <description>samsung smart phone</description>
</transaction>
</transactionlist>

I have parsed the element itempurchased in above xml and stored it in integer variable. How to validate itempurchased only for numbers. that is i want to check whether itempurchased is number. pls provide suggestions

Beryllium
  • 12,808
  • 10
  • 56
  • 86
user1526671
  • 817
  • 4
  • 16
  • 33
  • This is already answered in some other post - http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java – IndoKnight Sep 11 '13 at 09:18
  • regular expressions allow you to match the lexical representation against number formats. another track might be to convert the data to numbers using java classes and trapping errors with a specialized exception handler. last not least, you might employ schema validation (= type checking) on the xml level. there is a bunch of tools around, databases supporting xml storage might provide this functionality too (eg. oracle does). – collapsar Sep 11 '13 at 09:18

2 Answers2

2

the best way should be validate xml against an xsd, where itempurchased would be of type xsd:int

below the xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="transactionlist">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="transaction">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="transactionid" type="xs:string" />
              <xs:element minOccurs="0" name="transactiondate" type="xs:string" />
              <xs:element minOccurs="0" name="merchantdetails" type="xs:string" />
              <xs:element minOccurs="0" name="itempurchased" type="xs:int" />
              <xs:element minOccurs="0" name="amount" type="xs:int" />
              <xs:element minOccurs="0" name="description" type="xs:string" />
            </xs:sequence>
            <xs:attribute name="action" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Here Validating XML against XSD the code for validate xml against xsd

Community
  • 1
  • 1
Emanuele
  • 469
  • 2
  • 10
0

If you are marshaling your xml to a java bean then you may try using the Java6 Bean Validation Framework. Read more about it here:

http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html

It is as simple as putting an annotation on your bean:

public class MyXMLBean {
    @Max(10)
    @Min(5) 
    private int itempurchased;
}

The above bean will allow setting the value of itempurchased between min and max values mentioned in the annotations.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136