在Java中解析XML文件并输出所有错误

时间:2016-03-25 23:30:37

标签: java xml parsing error-handling xml-parsing

我目前正在尝试用Java解析XML文件。我已经做了很多研究,并尝试使用许多可用的库,如DOM,SAX等。所有这些都很棒并且有很多选择。我的问题是,我希望能够解析整个XML文件,然后输出可能存在的任何错误。以SAX为例,它将解析文件,但只输出第一个语法错误。有没有像编译器一样输出所有错误,或者我运气不好?

如果无法做到这一点,我正在尝试解析我的XML文件,就像普通文本文件一样,并抓住所有开始和结束标记。除了创建许多条件语句和规则之外,还有更简单的方法吗?

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:0)

只需使用自定义 public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "FCM Service"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { // TODO: Handle FCM messages here. // If the application is in the foreground handle both data and notification messages here. // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. Log.d(TAG, "From: " + remoteMessage.getFrom()); Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); sendNotification("deneme",remoteMessage.getNotification().getBody()); } int mNotificationId = 001; private void sendNotification(String messageTitle,String messageBody) { Intent notificationIntent = new Intent(this, LoginActivity.class); // set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); notificationBuilder.setSmallIcon(R.drawable.ic_launcher); notificationBuilder.setContentTitle(messageTitle); notificationBuilder.setContentText(messageBody); notificationBuilder.setAutoCancel(true); notificationBuilder.setContentIntent(resultPendingIntent); Notification notification = notificationBuilder.build(); NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); try { Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" + this.getPackageName() + "/raw/notification"); Ringtone r = RingtoneManager.getRingtone(this, alarmSound); r.play(); } catch (Exception e) { e.printStackTrace(); } notificationManager.notify(632623,notification ); } } (作为默认ErrorHandler在第一次出错时抛出异常)。

ErrorHandler

答案 1 :(得分:-1)

我认为您在找到这种类型的XML解析器方面会取得很大成功。根据定义,XML解析器应该在遇到的第一个错误时抛出异常。如果它没有,它将无法识别任何后续文本的节点类型或正确性......

相关问题