Last active
October 15, 2016 14:01
-
-
Save Viacheslav77/716c3c80cf5c6428c4b9068dc686d5da to your computer and use it in GitHub Desktop.
1. Сделать возможность искать значения в XML файле в случае дублирования имен тэгов. 2. Реализовать обработку тэгов типа <tag />.Рекурсия.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-> <catalog> | |
-> <book> | |
-> <author> | |
--------Gambardella, Matthew | |
-> <catalog> | |
-> <book> | |
-> <author> | |
--------Ralls, Kim | |
-> <catalog> | |
-> <book> | |
-> <genre> | |
-> <podgenre> | |
--------Fantazy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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