The java implementation USES the simple method of the String class to read the contents of a tag in the xml file

  • 2020-05-19 04:46:31
  • OfStack

1. Get specific content in a file quickly using indexOf() and substring() provided by the String class


public static void main(String[] args) {

		//  Test the position of a word 
		String reqMessage = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + "<in>" + "<head>" + "<Version>1.0.1</Version>"
				+ "<InstId>100000000000032</InstId>" + "<AnsTranCode>BJCEBQBIReq</AnsTranCode>" + "<TrmSeqNum>2012082008200931036344</TrmSeqNum>"
				+ "</head>" + "</in>";
		int indexbegin = reqMessage.indexOf("<TrmSeqNum>");
		int indexend = reqMessage.indexOf("</TrmSeqNum>");

		System.out.println("<TrmSeqNum> The location of the ===" + indexbegin);
		System.out.println("</TrmSeqNum> The location of the ===" + indexend);
		System.out.println(reqMessage.substring(indexbegin + 11, indexend)); // 11 is <TrmSeqNum> The length of the string 
	}

Related articles: