How to read xml file (java) - help

Category: Eclipse
 
aoxhui
2013-08-20 03:51:52

Sponsored Links
xml file:

<? xml version = "1.0" encoding = "UTF-8"?>
<classpath>
< classpathentry kind = "src" path = "src" />
<classpathentry kind = "con" path = "org.eclipse.jdt.launching.JRE_CONTAINER" / >
<classpathentry kind="output" path="bin"/>
</classpath>

question: I do not know how to the inside of the path with javax.w3c.dom are to find out, which added to a List. Do not know whether there are adequate guidance, I did not get off xml, battered now ...

Sponsored Links

langjs001
2013-08-20 04:10:59
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import ; java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax . InputSource;

public class TestClass {

/ **
* @ param args
* /
public static void main (String [] args) {
String xmlStr = readEncodeStringFromFile ("d: \ \ a.xml");
List <String> list = getInfoFromXMLString (xmlStr);
for (String str: list) {
System.out.println ("Path:" + str);
}
}

public static String readEncodeStringFromFile (String fielName) {
StringBuffer buffer ; = new StringBuffer ();
try {
FileInputStream fis = new FileInputStream (fielName);
InputStreamReader isr = new InputStreamReader (fis, "UTF-8");
Reader in = new BufferedReader (isr);
int ch;
while ((ch = in.read ())> -1) {
; buffer.append ((char) ch);
}
in.close ();
return buffer.toString ();
} catch ( IOException e) {
return null;
}
}

public static List <String> getInfoFromXMLString (String xmlStr) {
try {
ArrayList <String> result = new ArrayList <String> ();
Element rootNode = getDocRootElement (xmlStr);
if (rootNode! = null) {
NodeList nodeList = rootNode.getElementsByTagName ("classpathentry");
for (int i = 0; i <nodeList.getLength (); i + +) {
Element el = (Element) nodeList.item (i);
String path = el.getAttribute ("path");
if (safeString (path). length ()> 0)
result.add (path) ;
}

}
return result;
} catch (Exception e) {
return new ArrayList <String> ();
}
}

static public Element getDocRootElement (String inputXMLString) {

if (safeString (inputXMLString). length ( ) == 0)
return null;
Element root = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance ();
DocumentBuilder db = dbFactory.newDocumentBuilder ();

Document doc = db.parse (new InputSource (new StringReader (inputXMLString)));
root = doc.getDocumentElement ();
}
catch (Exception e) {
root = null;
}

return root;
}

public static String safeString (String str) {
if (str == null)
return " ";
else
return str;
}

}
jiaxingren
2013-08-20 04:14:16
Thank you very much, back to try. Thank
nikey502
2013-08-20 04:19:42
top and learn. . . .
boybyebye
2013-08-20 04:33:32
Thank you, in accordance with danielzhan way to solve it.
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.