Search in sources :

Example 26 with KXmlParser

use of org.kxml2.io.KXmlParser in project ETSMobile-Android2 by ApplETS.

the class ExtendedSoapSerializationEnvelope method GetSoapObject.

public SoapObject GetSoapObject(Element detailElement) {
    try {
        XmlSerializer xmlSerializer = XmlPullParserFactory.newInstance().newSerializer();
        StringWriter writer = new StringWriter();
        xmlSerializer.setOutput(writer);
        detailElement.write(xmlSerializer);
        xmlSerializer.flush();
        XmlPullParser xpp = new KXmlParser();
        xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        xpp.setInput(new StringReader(writer.toString()));
        xpp.nextTag();
        SoapObject soapObj = new SoapObject(detailElement.getNamespace(), detailElement.getName());
        readSerializable(xpp, soapObj);
        return soapObj;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) StringWriter(java.io.StringWriter) SoapObject(org.ksoap2.serialization.SoapObject) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 27 with KXmlParser

use of org.kxml2.io.KXmlParser in project android_frameworks_base by DirtyUnicorns.

the class ExpatPerformanceTest method runJavaPullParser.

private void runJavaPullParser() throws XmlPullParserException, IOException {
    XmlPullParser pullParser;
    long start = System.currentTimeMillis();
    pullParser = new KXmlParser();
    pullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    pullParser.setInput(newInputStream(), "UTF-8");
    withPullParser(pullParser);
    long elapsed = System.currentTimeMillis() - start;
    Log.i(TAG, "java pull parser: " + elapsed + "ms");
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser)

Example 28 with KXmlParser

use of org.kxml2.io.KXmlParser in project android by JetBrains.

the class DomPullParserTest method createReferenceParser.

private static KXmlParser createReferenceParser(PsiFile file) throws XmlPullParserException {
    KXmlParser referenceParser = new KXmlParser();
    referenceParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    referenceParser.setInput(new StringReader(file.getText()));
    return referenceParser;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) StringReader(java.io.StringReader)

Example 29 with KXmlParser

use of org.kxml2.io.KXmlParser in project android by JetBrains.

the class DomPullParserTest method compareParsers.

private static void compareParsers(PsiFile file, NextEventType nextEventType) throws Exception {
    assertTrue(file instanceof XmlFile);
    Document document = XmlUtils.parseDocumentSilently(file.getText(), true);
    assertNotNull(document);
    KXmlParser referenceParser = createReferenceParser(file);
    DomPullParser parser = new DomPullParser(document.getDocumentElement());
    assertEquals("Expected " + name(referenceParser.getEventType()) + " but was " + name(parser.getEventType()) + " (at line:column " + describePosition(referenceParser) + ")", referenceParser.getEventType(), parser.getEventType());
    while (true) {
        int expected, next;
        switch(nextEventType) {
            case NEXT:
                expected = referenceParser.next();
                next = parser.next();
                break;
            case NEXT_TOKEN:
                expected = referenceParser.nextToken();
                next = parser.nextToken();
                break;
            case NEXT_TAG:
                {
                    try {
                        expected = referenceParser.nextTag();
                    } catch (Exception e) {
                        expected = referenceParser.getEventType();
                    }
                    try {
                        next = parser.nextTag();
                    } catch (Exception e) {
                        next = parser.getEventType();
                    }
                    break;
                }
            default:
                throw new AssertionError("Unexpected type");
        }
        Element element = null;
        if (expected == XmlPullParser.START_TAG) {
            assertNotNull(parser.getViewCookie());
            assertTrue(parser.getViewCookie() == null || parser.getViewCookie() instanceof Element);
            element = (Element) parser.getViewCookie();
        }
        if (expected == XmlPullParser.START_TAG) {
            assertEquals(referenceParser.getName(), parser.getName());
            if (element != document.getDocumentElement()) {
                // KXmlParser seems to not include xmlns: attributes on the root tag!
                SortedSet<String> referenceAttributes = new TreeSet<>();
                SortedSet<String> attributes = new TreeSet<>();
                for (int i = 0; i < referenceParser.getAttributeCount(); i++) {
                    String s = referenceParser.getAttributePrefix(i) + ':' + referenceParser.getAttributeName(i) + '=' + referenceParser.getAttributeValue(i);
                    referenceAttributes.add(s);
                }
                for (int i = 0; i < parser.getAttributeCount(); i++) {
                    String s = parser.getAttributePrefix(i) + ':' + parser.getAttributeName(i) + '=' + parser.getAttributeValue(i);
                    attributes.add(s);
                    if (parser.getAttributeNamespace(i) != null) {
                        //noinspection ConstantConditions
                        assertEquals(normalizeValue(parser.getAttributeValue(i)), normalizeValue(parser.getAttributeValue(parser.getAttributeNamespace(i), parser.getAttributeName(i))));
                    }
                }
                assertEquals(referenceAttributes, attributes);
            }
            assertEquals(referenceParser.isEmptyElementTag(), parser.isEmptyElementTag());
            if (element instanceof XmlTag) {
                XmlTag tag = (XmlTag) element;
                for (XmlAttribute attribute : tag.getAttributes()) {
                    String namespace = attribute.getNamespace();
                    String name = attribute.getLocalName();
                    assertEquals(namespace + ':' + name + " in element " + parser.getName(), normalizeValue(referenceParser.getAttributeValue(namespace, name)), normalizeValue(parser.getAttributeValue(namespace, name)));
                }
            }
        } else if (expected == XmlPullParser.TEXT || expected == XmlPullParser.COMMENT) {
            assertEquals(StringUtil.notNullize(referenceParser.getText()).trim(), StringUtil.notNullize(parser.getText()).trim());
        }
        if (expected != next) {
            assertEquals("Expected " + name(expected) + " but was " + name(next) + "(At " + describePosition(referenceParser) + ")", expected, next);
        }
        if (expected == XmlPullParser.END_DOCUMENT) {
            break;
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) KXmlParser(org.kxml2.io.KXmlParser) TreeSet(java.util.TreeSet) XmlTag(com.intellij.psi.xml.XmlTag)

Example 30 with KXmlParser

use of org.kxml2.io.KXmlParser in project android by JetBrains.

the class LayoutPsiPullParserTest method createReferenceParser.

private static KXmlParser createReferenceParser(PsiFile file) throws XmlPullParserException {
    KXmlParser referenceParser = new KXmlParser();
    referenceParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    referenceParser.setInput(new StringReader(file.getText()));
    return referenceParser;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) StringReader(java.io.StringReader)

Aggregations

KXmlParser (org.kxml2.io.KXmlParser)32 XmlPullParser (org.xmlpull.v1.XmlPullParser)14 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)14 StringReader (java.io.StringReader)8 IOException (java.io.IOException)6 URL (java.net.URL)3 URLConnection (java.net.URLConnection)3 DocumentImpl (org.apache.harmony.xml.dom.DocumentImpl)3 DocumentType (org.w3c.dom.DocumentType)3 SAXParseException (org.xml.sax.SAXParseException)3 LocatorImpl (org.xml.sax.helpers.LocatorImpl)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlFile (com.intellij.psi.xml.XmlFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 InputStreamReader (java.io.InputStreamReader)2 TreeSet (java.util.TreeSet)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 PsiElement (com.intellij.psi.PsiElement)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1