Search the web
Sign In
New User? Sign Up
xmlpull-user
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Show off your group to the world. Share a photo of your group with us.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Re: Modify attributes   Message List  
Reply | Forward Message #213 of 308 |
Re: Modify attributes

operaska wrote:

>Hi
>
>It would be achieved with
>Pp.setInput (new StringReader (stringXml.trim ()));
>Please, You might explain the example (Roundtrip), I do not
>understand it.
>
>
study API, samples, and read about xmlpull:
http://www.cafeconleche.org/books/xmljava/chapters/ch05s09.html
and if you do not understand XML parsing then read whole book
http://www.cafeconleche.org/books/xmljava/

HTH,

alek

ps. here is working example that does what you want

/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
//------100-columns-wide------>|*/
// for license please see accompanying LICENSE.txt file (available also
at http://www.xmlpull.org/)


//package org.xmlpull.v1.samples;

import java.io.*;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import org.xmlpull.v1.XmlSerializer;

/**
* This sample demonstrates how to roundtrip XML document
* (roundtrip is not exact but infoset level)
*/

public class RoundtripAndCheck {

private final static String PROPERTY_XMLDECL_STANDALONE =
"http://xmlpull.org/v1/doc/features.html#xmldecl-standalone";
private final static String PROPERTY_SERIALIZER_INDENTATION =
"http://xmlpull.org/v1/doc/properties.html#serializer-indentation";

private XmlPullParser parser;
private XmlSerializer serializer;

private RoundtripAndCheck(XmlPullParser parser, XmlSerializer
serializer) {
this.parser = parser;
this.serializer = serializer;
}

private void writeStartTag() throws XmlPullParserException,
IOException {
//check for case when feature xml roundtrip is supported
//if (parser.getFeature (FEATURE_XML_ROUNDTRIP)) {

if(!parser.getFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES)) {
for(int i = parser.getNamespaceCount(parser.getDepth() - 1);
i <= parser.getNamespaceCount(parser.getDepth()) - 1; i++) {
serializer.setPrefix
(parser.getNamespacePrefix(i),
parser.getNamespaceUri(i));
}
}
serializer.startTag(parser.getNamespace(), parser.getName());

for(int i = 0; i < parser.getAttributeCount(); i++) {
if(parser.getAttributeName(i).equals("date")) {
serializer.attribute
(parser.getAttributeNamespace(i),
parser.getAttributeName(i),
"12-12-05:12:00:00");
} else {
serializer.attribute
(parser.getAttributeNamespace(i),
parser.getAttributeName(i),
parser.getAttributeValue(i));
}
}
//serializer.closeStartTag();
}


private void writeToken(int eventType) throws
XmlPullParserException, IOException {
switch(eventType) {
case XmlPullParser.START_DOCUMENT:
//use Boolean.TRUE to make it standalone
Boolean standalone = (Boolean)
parser.getProperty(PROPERTY_XMLDECL_STANDALONE);
serializer.startDocument(parser.getInputEncoding(),
standalone);
break;

case XmlPullParser.END_DOCUMENT:
serializer.endDocument();
break;

case XmlPullParser.START_TAG:
writeStartTag();
break;

case XmlPullParser.END_TAG:
serializer.endTag(parser.getNamespace(), parser.getName());
break;

case XmlPullParser.IGNORABLE_WHITESPACE:
//comment it to remove ignorable whtespaces from XML infoset
String s = parser.getText();
serializer.ignorableWhitespace(s);
break;

case XmlPullParser.TEXT:
serializer.text(parser.getText());
break;

case XmlPullParser.ENTITY_REF:
serializer.entityRef(parser.getName());
break;

case XmlPullParser.CDSECT:
serializer.cdsect(parser.getText());
break;

case XmlPullParser.PROCESSING_INSTRUCTION:
serializer.processingInstruction(parser.getText());
break;

case XmlPullParser.COMMENT:
serializer.comment(parser.getText());
break;

case XmlPullParser.DOCDECL:
serializer.docdecl(parser.getText());
break;
}
}

private void roundTrip() throws XmlPullParserException, IOException {
parser.nextToken(); // read first token
writeToken(XmlPullParser.START_DOCUMENT); // write optional
XMLDecl if present
while(parser.getEventType() != XmlPullParser.END_DOCUMENT) {
writeToken(parser.getEventType());
parser.nextToken();
}
writeToken(XmlPullParser.END_DOCUMENT);
}

private static void roundTrip(Reader reader, Writer writer, String
indent)
throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser pp = factory.newPullParser();
pp.setInput(reader);
XmlSerializer serializer = factory.newSerializer();
serializer.setOutput(writer);
if(indent != null) {
serializer.setProperty(PROPERTY_SERIALIZER_INDENTATION, indent);
}
(new RoundtripAndCheck(pp, serializer)).roundTrip();

}

private static void roundTrip(XmlPullParserFactory factory, String loc)
throws XmlPullParserException, IOException {
roundTrip(factory, loc, null);
}

private static void roundTrip(XmlPullParserFactory factory, String
loc, String indent)
throws XmlPullParserException, IOException {
XmlPullParser pp = factory.newPullParser();
pp.setInput(new java.net.URL(loc).openStream(), null);

XmlSerializer serializer = factory.newSerializer();
serializer.setOutput(System.out, null);
if(indent != null) {
serializer.setProperty(PROPERTY_SERIALIZER_INDENTATION, indent);
}
(new RoundtripAndCheck(pp, serializer)).roundTrip();
}

// public static void main(String[] args) throws Exception {
// //for (int i = 0; i < args.length; i++)
// XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// factory.setNamespaceAware(true);
// for(int i = 0; i < 1; i++) {
// roundTrip(factory, args[i], " ");
// }
// }

public static void main(String[] args) throws Exception {
String XML = "<test><foo date='yesterday'>fdf</foo></test>";
Reader r = new StringReader(XML);
Writer w = new StringWriter();
//roundTrip(r, w, " ");
roundTrip(r, w, null);
System.out.println("XML="+w);
}

}



--
The best way to predict the future is to invent it - Alan Kay




Wed May 4, 2005 8:24 pm

as10m
Offline Offline
Send Email Send Email

Forward
Message #213 of 308 |
Expand Messages Author Sort by Date

... XmlPullParser parser can not modify anything (it is only reading) - you need XmlSerializer (to write), check this example: ...
Aleksander Slominski
as10m
Offline Send Email
May 4, 2005
4:36 pm

Hello Thanks in advance I try this and generate java.lang.IllegalArgumentException: startTag() must be called before attribute() What i can do? ... you ... ...
operaska
Offline Send Email
May 4, 2005
6:00 pm

... did you get unmodified Roundtrip to work? i am pretty sure it works. makes sure you use the latest version of xmlpull compatible parser (i test it with...
Aleksander Slominski
as10m
Offline Send Email
May 4, 2005
6:12 pm

Thank you for answering My question is the following one: I have a String with format XML, to which I want to validate him the date that is an attribute of the...
operaska
Offline Send Email
May 4, 2005
7:11 pm

... modify Roundtrip to have parser read from string (StringReader) and serializer to write back XML into string (StringWriter) alek ... -- The best way to...
Aleksander Slominski
as10m
Offline Send Email
May 4, 2005
7:19 pm

... study API, samples, and read about xmlpull: http://www.cafeconleche.org/books/xmljava/chapters/ch05s09.html and if you do not understand XML parsing then...
Aleksander Slominski
as10m
Offline Send Email
May 4, 2005
8:24 pm

Many Many Many Thanks, xpp is the best parser XML that I have seen. Thank you again Please he looks at this: ...
operaska
Offline Send Email
May 4, 2005
9:54 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help