Description
Copy an XML fragment from a source (pull parser) to a destination (serializer), pulling all element namespaces into the empty prefix.
License
LGPL
Parameter
Parameter | Description |
---|
xmlPullParser | The pull parser for xml reading. |
xmlSerializer | The serializer for xml writing. |
Exception
Parameter | Description |
---|
XmlPullParserException | In case of invalid XML. |
IOException | In case of an closes connection. |
Declaration
public final static void copyXML(XmlPullParser xmlPullParser,
XmlSerializer xmlSerializer) throws XmlPullParserException,
IOException
Method Source Code
//package com.java2s;
/*//ww w. j a v a 2 s. c o m
* Licensed under Apache License, Version 2.0 or LGPL 2.1, at your option.
* --
*
* Copyright 2010 Rene Treffer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* --
*
* Copyright (C) 2010 Rene Treffer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
public class Main {
/**
* Copy an XML fragment from a source (pull parser) to a destination
* (serializer), pulling all element namespaces into the empty prefix.
*
* Errata: Some common XMPP servers (read: ejabberd) don't handle stanzas
* like <n0:iq to="romeo@example.com" xmlns:n0="jabber:client">
* this means we'll have to rename "n0" to "", which isn't the
* default behaviour.
*
* @param xmlPullParser The pull parser for xml reading.
* @param xmlSerializer The serializer for xml writing.
* @throws XmlPullParserException In case of invalid XML.
* @throws IOException In case of an closes connection.
*/
public final static void copyXML(XmlPullParser xmlPullParser,
XmlSerializer xmlSerializer) throws XmlPullParserException,
IOException {
int startDepth = xmlPullParser.getDepth();
String namespace = null;
do {
int type = xmlPullParser.next();
switch (type) {
case XmlPullParser.END_TAG:
xmlSerializer.endTag(xmlPullParser.getNamespace(),
xmlPullParser.getName());
break;
case XmlPullParser.START_TAG:
if (!xmlPullParser.getNamespace().equals(namespace)) {
namespace = xmlPullParser.getNamespace();
xmlSerializer.setPrefix("", namespace);
}
xmlSerializer.startTag(xmlPullParser.getNamespace(),
xmlPullParser.getName());
int attributeCount = xmlPullParser.getAttributeCount();
for (int i = 0; i < attributeCount; i++) {
xmlSerializer.attribute(
xmlPullParser.getAttributeNamespace(i),
xmlPullParser.getAttributeName(i),
xmlPullParser.getAttributeValue(i));
}
break;
case XmlPullParser.TEXT:
xmlSerializer.text(xmlPullParser.getText());
break;
case XmlPullParser.END_DOCUMENT:
// We shouldnt see that!
throw new XmlPullParserException(
"Unexpected end of stream.");
default:
throw new IllegalStateException(
"Unexpected pull parser type " + type);
}
} while (xmlPullParser.getDepth() > startDepth);
}
}
Related
- optAttr(final XmlPullParser pp, final String attrName, final String defaultValue)
- optFloatAttr(final XmlPullParser pp, final String attrName, final float defaultValue)
- optIntAttr(final XmlPullParser pp, final String attrName, final int defaultValue)
- optSkip(final XmlPullParser pp, final String tagName)
- attr(final XmlPullParser pp, final String attrName)
- exit(final XmlPullParser pp, final String tagName)
- floatAttr(final XmlPullParser pp, final String attrName)
- intAttr(final XmlPullParser pp, final String attrName)