XmlPullParser:在Tag中提取标签

时间:2015-06-11 08:33:23

标签: xml tags attributes extract xmlpullparser

我想在使用XmlPullParser的父母标签中添加几个标签,但我不知道如何做到这一点。

这是我的XML响应:

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>

这是我的解析器:

  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;

    }

我想提取一下:

<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>

怎么做?

谢谢你的回复! :)

1 个答案:

答案 0 :(得分:0)

我发现:我使用过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;
    }