org.xmlpull.v1.XmlPullParserFactory类的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(12.3k)|赞(0)|评价(0)|浏览(323)

本文整理了Java中org.xmlpull.v1.XmlPullParserFactory类的一些代码示例,展示了XmlPullParserFactory类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XmlPullParserFactory类的具体详情如下:
包路径:org.xmlpull.v1.XmlPullParserFactory
类名称:XmlPullParserFactory

XmlPullParserFactory介绍

[英]This class is used to create implementations of XML Pull Parser defined in XMPULL V1 API. The name of actual factory class will be determined based on several parameters. It works similar to JAXP but tailored to work in J2ME environments (no access to system properties or file system) so name of parser class factory to use and its class used for loading (no class loader - on J2ME no access to context class loaders) must be passed explicitly. If no name of parser factory was passed (or is null) it will try to find name by searching in CLASSPATH for META-INF/services/org.xmlpull.v1.XmlPullParserFactory resource that should contain a comma separated list of class names of factories or parsers to try (in order from left to the right). If none found, it will throw an exception.
NOTE:In J2SE or J2EE environments, you may want to use newInstance(property, classLoaderCtx) where first argument is System.getProperty(XmlPullParserFactory.PROPERTY_NAME) and second is Thread.getContextClassLoader().getClass() .
[中]此类用于创建XMPULL V1 API中定义的XML Pull解析器的实现。实际工厂类的名称将根据几个参数确定。它的工作原理与JAXP类似,但为在J2ME环境中工作而定制(无法访问系统属性或文件系统),因此必须显式传递要使用的解析器类工厂的名称及其用于加载的类(没有类加载器——在J2ME上无法访问上下文类加载器)。如果没有传递解析器工厂的名称(或为null),它将尝试通过在类路径中搜索META-INF/services/org来查找名称。拉。v1。XmlPullParserFactory资源,它应该包含一个逗号分隔的列表,列出要尝试的工厂或解析器的类名(按从左到右的顺序)。如果找不到,它将抛出异常。
注意:在J2SE或J2EE环境中,您可能希望使用newInstance(property, classLoaderCtx),其中第一个参数是System.getProperty(XmlPullParserFactory.PROPERTY_NAME),第二个参数是[$2$]。

代码示例

代码示例来源:origin: k9mail/k-9

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
InputStreamReader reader = new InputStreamReader(inputStream);
xpp.setInput(reader);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
  if (eventType == XmlPullParser.START_TAG) {
    if (SettingsExporter.ROOT_ELEMENT.equals(xpp.getName())) {
      imported = parseRoot(xpp, globalSettings, accountUuids, overview);
    } else {

代码示例来源:origin: googlemaps/android-maps-utils

/**
 * Creates a new XmlPullParser to allow for the KML file to be parsed
 *
 * @param stream InputStream containing KML file
 * @return XmlPullParser containing the KML file
 * @throws XmlPullParserException if KML file cannot be parsed
 */
private static XmlPullParser createXmlParser(InputStream stream) throws XmlPullParserException {
  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  factory.setNamespaceAware(true);
  XmlPullParser parser = factory.newPullParser();
  parser.setInput(stream, null);
  return parser;
}

代码示例来源:origin: google/ExoPlayer

public TtmlDecoder() {
 super("TtmlDecoder");
 try {
  xmlParserFactory = XmlPullParserFactory.newInstance();
  xmlParserFactory.setNamespaceAware(true);
 } catch (XmlPullParserException e) {
  throw new RuntimeException("Couldn't create XmlPullParserFactory instance", e);
 }
}

代码示例来源:origin: robovm/robovm

/**
 */
public Driver() throws XmlPullParserException {
  final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  factory.setNamespaceAware(true);
  pp = factory.newPullParser();
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

/**
 * Create a new XmlPullParser using the XPP factory.
 * 
 * @return a new parser instance
 * @throws XmlPullParserException if the factory fails
 * @since 1.4.1
 */
public static XmlPullParser createDefaultParser() throws XmlPullParserException {
  XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
  return factory.newPullParser();
}

代码示例来源:origin: stackoverflow.com

URL sourceUrl = new URL(sourceUrlString);
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(global.getInputStream(sourceUrl), "UTF_8");
if (xpp.getEventType() == XmlPullParser.START_TAG)
  if (xpp.getName().equalsIgnoreCase("title"))
    sourceTitle = xpp.nextText();
  succeed = true;

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
 InputStream in = new BufferedInputStream(urlConnection.getInputStream());
 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
 factory.setNamespaceAware(true);
 XmlPullParser parser = factory.newPullParser(); 
 parser.setInput(new InputStreamReader(in));
 XmlUtils.beginDocument(parser,"results");
 int eventType = parser.getEventType();
 do{
  XmlUtils.nextElement(parser);
  parser.next();
  eventType = parser.getEventType();
  if(eventType == XmlPullParser.TEXT){
   Log.d("test",parser.getText());
  }
  //...Handle other types of events...
 } while (eventType != XmlPullParser.END_DOCUMENT) ;
}
finally {
 urlConnection.disconnect();
}

代码示例来源:origin: org.ogce/xpp3

public static void main(String [] args)
{
  // create an instance of RSSReader
  RSSReader rssreader = new RSSReader();
  try {
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    XmlPullParser parser = factory.newPullParser();
    String url = args[0];
    InputStreamReader stream = new InputStreamReader(
      new URL(url).openStream());
    parser.setInput(stream);
    XmlSerializer writer = factory.newSerializer();
    writer.setOutput(new OutputStreamWriter(System.out));
    rssreader.convertRSSToHtml(parser, writer);
  } catch (Exception e) {
    e.printStackTrace(System.err);
  }
}

代码示例来源:origin: stackoverflow.com

try {
   items = new ArrayList<String>();
   XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
   factory.setNamespaceAware(true);
   XmlPullParser xpp = factory.newPullParser();
   xpp.setInput(new InputStreamReader(
       getUrlData(" url")));
   while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
     Log.i(TAG, "doc started");
     if (xpp.getEventType() == XmlPullParser.START_TAG) {
       if (xpp.getName().equals("entry")) {
         items.add(xpp.getAttributeValue(0));
       }
     }
     xpp.next();
   }
 } catch (Throwable t) {
   Toast.makeText(this, "Request failed: " + t.toString(),
       Toast.LENGTH_LONG).show();
 }

代码示例来源:origin: stackoverflow.com

HttpURLConnection httpURLConnection = null;
try {
  url = new URL(GetData.NonOpDomain);
  httpURLConnection = (HttpURLConnection) url.openConnection();
  do {
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setRequestProperty("Connection", "keep-alive");
    httpURLConnection.setRequestProperty("Content-Type", "text/xml");
    httpURLConnection.setRequestProperty("SendChunked", "True");
    httpURLConnection.setRequestProperty("UseCookieContainer", "True");
    HttpURLConnection.setFollowRedirects(false);
      InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
      XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
      XmlPullParser myparser = xmlFactoryObject.newPullParser();
      myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
      myparser.setInput(is, null);
      parseXMLAndStoreIt(myparser);
    } catch (Exception e1) {

代码示例来源:origin: stackoverflow.com

String text=null;
  try {
  event = myParser.getEventType();
   while (event != XmlPullParser.END_DOCUMENT) {
   String name=myParser.getName();
    switch (event){
    case XmlPullParser.START_TAG:
    break;
   case XmlPullParser.TEXT:
     text = myParser.getText();
   break;
   case XmlPullParser.END_TAG:
public void run() {
try {
  URL url = new URL(urlString);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setReadTimeout(10000 /* milliseconds */);
  conn.setConnectTimeout(15000 /* milliseconds */);
  conn.setRequestMethod("GET");
  conn.setDoInput(true);
  conn.connect();
  InputStream stream = conn.getInputStream();
  xmlFactoryObject = XmlPullParserFactory.newInstance();
  XmlPullParser myparser = xmlFactoryObject.newPullParser();
  myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
  myparser.setInput(stream, null);

代码示例来源:origin: stackoverflow.com

URL url = new URL(RSS_URL);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-agent", "Mozilla/4.0");
connection.setConnectTimeout(20000);
connection.connect();
InputStream in = connection.getInputStream();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(in, null);
String description = "";
String pubDate = "";
eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
  if (eventType == XmlPullParser.START_TAG) {
    String tag = xpp.getName();
    if (tag.equals("item")) {
      title = link = description = "";

代码示例来源:origin: stackoverflow.com

url = new URL(xmlUrl);
  parserFactory.setNamespaceAware(false);
  XmlPullParser parser = parserFactory.newPullParser();
  parser.setInput(getInputStream(url), "UTF_8");
  int eventType = parser.getEventType();
      if (parser.getName().equalsIgnoreCase("item")) {
        insideItem = true;
      } else if (parser.getName().equalsIgnoreCase("title")) {
InputStream inputStream = null;
try {
  URLConnection connection = url.openConnection();
  connection.setConnectTimeout(15000);
  connection.setReadTimeout(10000);
  connection.setDoInput(true);
  connection.connect();

代码示例来源:origin: stackoverflow.com

InputStream inputStream;
try {
  URL input = new URL("http://....");
  connection = (HttpURLConnection) input.openConnection();
} catch (IOException e) {
  exception = e;
  XmlPullParser parser = factory.newPullParser();
  connection.setRequestProperty("Accept", "application/xml");
  inputStream = connection.getInputStream();
  parser.setInput(inputStream, "UTF-8");
  ...

代码示例来源:origin: stackoverflow.com

URL url = new URL("http://www.cricinfo.com/rss/content/story/feeds/6.xml");//parsing a sample url
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {

  if (xpp.getName().equalsIgnoreCase("item")) {//first tag is item
    insideItem = true;
  }
}
}

 public InputStream getInputStream(URL url) {  //open connection to url
    try {
      return url.openConnection().getInputStream();
    } catch (IOException e) {
      return null;
     }

代码示例来源:origin: arunkumar9t2/rxSuggestions

@NonNull
@Override
public Flowable<SimpleSuggestionItem> getSuggestions(@NonNull String value) {
  return Flowable.fromCallable(() -> {
    final String suggestUrl = String.format(SUGGEST_URL_FORMAT, Util.prepareSearchTerm(value));
    final URL url = new URL(suggestUrl);
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.connect();
    return httpURLConnection;
  }).flatMap(httpURLConnection -> Flowable.create(emitter -> {
    try (final InputStream inputStream = httpURLConnection.getInputStream()) {
      final XmlPullParser xmlParser = XmlPullParserFactory.newInstance().newPullParser();
      xmlParser.setInput(inputStream, Util.extractEncoding(httpURLConnection.getContentType()));
      int eventType = xmlParser.getEventType();
      while (isFlowableEmissionValid(emitter, eventType)) { // Perform back pressure aware iteration.
        boolean validEvent = eventType == START_TAG && xmlParser.getName().equalsIgnoreCase(SUGGESTION);
        if (validEvent) {
          final String suggestion = xmlParser.getAttributeValue(0);
          emitter.onNext(new SimpleSuggestionItem(suggestion));
        }
        eventType = xmlParser.next();
      }
      emitter.onComplete();
    } finally {
      cancel(httpURLConnection);
    }
    emitter.setCancellable(() -> cancel(httpURLConnection));
  }, BackpressureStrategy.LATEST));
}

代码示例来源:origin: stackoverflow.com

URL url = new URL(XML_INIT_ADRESS);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(url.openStream(), null);

代码示例来源:origin: org.ogce/xpp3

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 Roundtrip(pp, serializer)).roundTrip();
}

代码示例来源:origin: igniterealtime/Smack

public MacroGroup parseMacroGroups(String macros) throws XmlPullParserException, IOException {
    MacroGroup group = null;
    XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
    parser.setInput(new StringReader(macros));
    int eventType = parser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
      eventType = parser.next();
       if (eventType == XmlPullParser.START_TAG) {
          if (parser.getName().equals("macrogroup")) {
            group = parseMacroGroup(parser);
          }
       }
    }
    return group;
  }
}

代码示例来源:origin: aws-amplify/aws-sdk-android

private StaxUnmarshallerContext getContext(String xml, Map<String, String> headers)
    throws Exception {
  XmlPullParser xpp = factory.newPullParser();
  Reader reader = new StringReader(xml);
  xpp.setInput(reader);
  return new StaxUnmarshallerContext(xpp, headers);
}

相关文章