Java tutorial
//package com.java2s; /* * eXist Open Source Native XML Database * Copyright (C) 2001-2014, Wolfgang M. Meier (meier@ifs.tu-darmstadt.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * as published by the Free Software Foundation; either version 2 * 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 Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: */ import java.io.ByteArrayOutputStream; public class Main { public static final String getXMLDecl(final byte[] data) { boolean foundTag = false; for (int i = 0; i < data.length && !foundTag; i++) { if (data[i] == '<') { foundTag = true; /* * Need to gather the next 4 non-zero values and test them * because greater than 8-bytes character encodings will be * represented with two bits */ boolean foundQuestionMark = false; int placeInDeclString = 0; final byte[] declString = new byte[4]; int x = (i + 1); for (; x < data.length; x++) { if (data[x] == 0) { continue; } if (!foundQuestionMark && data[x] != '?') { break; } else { foundQuestionMark = true; } declString[placeInDeclString] = data[x]; placeInDeclString++; if (placeInDeclString >= 4) { break; } } if (placeInDeclString == 4 && declString[0] == '?' && declString[1] == 'x' && declString[2] == 'm' && declString[3] == 'l') { final ByteArrayOutputStream out = new ByteArrayOutputStream(150); out.write('<'); out.write(declString, 0, 4); for (int j = (x + 1); j < data.length; j++) { if (data[j] != 0) { out.write(data[j]); } if (data[j] == '?') { j++; /* * When we find this we have to start looking for the end tag */ for (; j < data.length; j++) { if (data[j] == 0) { continue; } out.write(data[j]); if (data[j] != '>') { break; } return new String(out.toByteArray()); } } } } } } return null; } }