XmlPullParser : extract tags in Tag

111 views Asked by At

i would like to have several Tags in a Parents Tag using XmlPullParser but i don't know how to do this.

This is my XML Response :

RESPONSE:
<?xml version="1.0"  encoding="UTF-8" ?>
<SecretTAG Version="2.0">
 <Program name="FCVTNW">
   ***<ReturnValue Type="Char">TWELVE THOUSAND, THREE HUNDRED FORTY FIVE DOLLARS AND NO CENTS</ReturnValue>
   <ParameterList>
     <LIMIT Type="Packed">150</LIMIT>
     <DECV Type="Packed">12345.00</DECV>
     <ERROR Type="Char"></ERROR>***
     </ParameterList>
   </Program>

 <End Time="2015-05-29 15:30:37.000" Elapsed="110" Clock="1"/>
 <Info Built="02.00.002 Jan  6 2015 15:46:19 - i5/520"/>
 </SecretTag>

And this is my parser :

  public void parse(String xml) throws XmlPullParserException, IOException {
            // convert String into InputStream
            // Does not work
            InputStream in = new ByteArrayInputStream(xml.getBytes()); 
            try {
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = factory.newPullParser();//Xml.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in, null);
                parser.nextTag();
                readEntry(parser);

            } finally {
                in.close();
            }
        }

    private void readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
            parser.require(XmlPullParser.START_TAG, ns, "Easycom");
            this.Program = null;
            this.ReturnValue = null;
            this.ParameterList = null;
            this.FATAL = null;
            this.End = null;
            do {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                if (name.equals("Program")) {
                    this.Program = readProgram(parser);
                    parser.next();
                } else if (name.equals("ReturnValue")) {
                    this.ReturnValue = readReturnValue(parser);
                    parser.next();
                } else if (name.equals("ParameterList")) {
                    this.ParameterList = readParameterList(parser);
                    parser.nextTag();
                } else if (name.equalsIgnoreCase("FATAL")) {
                    this.FATAL = readFATAL(parser);
                    parser.next();
                } else if (name.equals("End")) {
                    this.End = readEnd(parser);
                    parser.next();
                } else {    
                    parser.next();
                }

            }while(parser.next() != XmlPullParser.END_TAG);

    private String readParameterList(XmlPullParser parser) throws IOException, XmlPullParserException {

            parser.require(XmlPullParser.START_TAG, ns, "ParameterList");
            String ParameterList = readText(parser);
            parser.require(XmlPullParser.END_TAG, ns, "ParameterList");
            return ParameterList;
    }


private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";

             if (parser.next() == XmlPullParser.TEXT) {
                result = parser.getText();
                parser.nextTag();
            }
        return result;

    }

I would like to extract this :

<ReturnValue Type="Char">TWELVE THOUSAND, THREE HUNDRED FORTY FIVE DOLLARS AND NO CENTS</ReturnValue>
       <ParameterList>
         <LIMIT Type="Packed">150</LIMIT>
         <DECV Type="Packed">12345.00</DECV>
         <ERROR Type="Char"></ERROR>***
         </ParameterList>

How to do this ?

thank you for your responses ! :)

1

There are 1 answers

0
Yvan Séry On BEST ANSWER

i have found : I have used StringBuilder.

private String readParameterList(XmlPullParser parser) throws IOException, XmlPullParserException {
            parser.require(XmlPullParser.START_TAG, ns, "ParameterList");
            StringBuilder builder = new StringBuilder();
            String result =null;
             while (!(parser.next() == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("ParameterList")) ) {
                 String ParameterList = parser.getText();

                  result = builder.append(ParameterList).toString();//.append(" ")
             }
            //String ParameterList =readText(parser);
            //parser.require(XmlPullParser.END_TAG, ns, "ParameterList");
             result = result.replaceAll("\\n", "");
            return result;
    }