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

2
  • This is already answered in some other post - stackoverflow.com/questions/5439529/… Commented Sep 11, 2013 at 9: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). Commented Sep 11, 2013 at 9:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.