Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active October 15, 2016 14:01
Show Gist options
  • Save Viacheslav77/716c3c80cf5c6428c4b9068dc686d5da to your computer and use it in GitHub Desktop.
Save Viacheslav77/716c3c80cf5c6428c4b9068dc686d5da to your computer and use it in GitHub Desktop.
1. Сделать возможность искать значения в XML файле в случае дублирования имен тэгов. 2. Реализовать обработку тэгов типа <tag />.Рекурсия.
<?xml version="1.0"?>
<catalog>
<book>
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book>
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>
<podgenre>Fantazy</podgenre>
</genre>
<price>5.95</price>
<otv/>
<publish_date>2000-12-16</publish_date>
</book>
</catalog>
-> <catalog>
-> <book>
-> <author>
--------Gambardella, Matthew
-> <catalog>
-> <book>
-> <author>
--------Ralls, Kim
-> <catalog>
-> <book>
-> <genre>
-> <podgenre>
--------Fantazy
package com.comp.Simple.XML.Parser;
import java.util.ArrayList;
class FindNumberTags {
private static ArrayList <String> textRez = new ArrayList<String>();
public static ArrayList <String> getRez (){
return textRez;
}
public static void getClin (){
textRez.clear();
}
public static void FindTags (String teg, String s_xml){
String toFind = "<" + teg + ">";
int start = s_xml.indexOf(toFind);
int end = s_xml.indexOf("</" + teg + ">");
if (start > 0 && end >0) {
start += toFind.length();
textRez.add(s_xml.substring(start, end));
s_xml = s_xml.substring(end+("</" + teg + ">").length());
}
start = s_xml.indexOf(toFind);
if (start > 0)
FindTags (teg, s_xml);
}
}
package com.comp.Simple.XML.Parser;
/* 1. Сделать возможность искать значения в случае дублирования имен тэгов.
2. Реализовать обработку тэгов типа <tag />
*/
class Main {
public static void main(String[] args) {
SimpleXMLParser xml = new SimpleXMLParser("D:\\1\\1.xml");
xml.get("catalog/book/author");
xml.get("catalog/book/otv");
xml.get("catalog/book/genre/podgenre");
}
}
package com.comp.Simple.XML.Parser;
import java.io.*;
class SimpleXMLParser {
private String xml;
public SimpleXMLParser(String path) {
byte[] buf = null;
try (RandomAccessFile file = new RandomAccessFile(path, "r")){
buf = new byte[(int)file.length()];
file.read(buf);
} catch (IOException e) {
e.printStackTrace();
}
try {
xml = new String(buf, "US-ASCII");
} catch (UnsupportedEncodingException ex) {}
}
public void get(String path) {
String[] parts = path.split("/");
for (String s : parts){
FindNumberTags.getClin();
FindNumberTags.FindTags(s, xml);
}
for (String s: FindNumberTags.getRez())
System.out.println(printPath(path) +"--------"+s);
FindNumberTags.getClin();
}
public String printPath(String path){
StringBuilder newPath = new StringBuilder();
String[] parts = path.split("/");
String p = "";
for (String s : parts){
p += " ";
newPath .append(" -> ")
.append("<"+ s +">")
.append("\n")
.append(p);
}
return newPath.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment