import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class Xml {
public static void main(String[] args) throws Exception
{
File fXmlFile = new File("C:\\Users\\sherkat\\Desktop\\proje\\test.xml");
InputStream inputStream= new FileInputStream(fXmlFile);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("file");
Node $t = nList.item(0);
Element t = (Element)$t;
String str = t.getElementsByTagName("fail").item(0).getTextContent();
System.out.println(str);
}
}
بالا کد من
فایل xml هم اینو نوشتم
<؟xml version = "1.0"?>
<file>
<fail>
فایل
</fail>
</file>
به جای فایل چهارتا مربع یا علامت سوال چاپ میکنه