try { StringBuffer strFile = new StringBuffer(); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File(inputfile)); doc.getDocumentElement ().normalize (); NodeList listOfDoc = doc.getElementsByTagName("writer"); for(int s=0; s<listOfDoc.getLength() ; s++){ Element firstDocNode = (Element) listOfDoc.item(s); String name= firstDocNode.getAttribute("Name"); System.out.println(name); } }catch (Throwable t) { t.printStackTrace (); }
input.xml
<?xml version="1.0"?> <Document> <Title> <writer Name="A"/> <writer Name="B"/> <writer Name="C"/> <writer Name="D"/> <writer Name="E"/> <writer Name="F"/> <writer Name="G"/> <writer Name="H"/> </Title> </Document>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น