forked from quanke/design-pattern-java-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLUtilTV.java
More file actions
30 lines (29 loc) · 845 Bytes
/
Copy pathXMLUtilTV.java
File metadata and controls
30 lines (29 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;
public class XMLUtilTV
{
//该方法用于从XML配置文件中提取品牌名称,并返回该品牌名称
public static String getBrandName()
{
try
{
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("configTV.xml"));
//获取包含品牌名称的文本节点
NodeList nl = doc.getElementsByTagName("brandName");
Node classNode=nl.item(0).getFirstChild();
String brandName=classNode.getNodeValue().trim();
return brandName;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}