Xml Java解析
我正在尝试解析这个Xml Java解析,java,xml,parsing,Java,Xml,Parsing,我正在尝试解析这个.xml文件来提取歌曲名,我已经尝试过了 public class StudentDOMParser { public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newD
.xml
文件来提取歌曲名,我已经尝试过了
public class StudentDOMParser {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("itunes3.xml");
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());
System.out.println("============================");
NodeList nList = document.getElementsByTagName("dict");
// NodeList xList = document.getElementsByTagName("student");
// Iterate through each employee and print their details
for (int i = 0; i < nList.getLength(); i++) {
// Extract each individual employee
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
System.out.println("song name : " + eElement.getElementsByTagName("string").item(0).getTextContent());
// System.out.println("song name : " + eElement.getFirstChild().getTextContent());
}
//System.out.println("college name : " + eElement.getElementsByTagName("Track").item(0).getTextContent());
}
}
}
关于如何解析这首歌以获得“比阳光更明亮”的歌曲名的建议使用XPath获取所需数据可能是一种更有效的选择 下面的示例使用XPath从xml获取所有plist/dict/dict/dict/string
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(ResourceUtils.getFile("/itunes.xml").getAbsolutePath());
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());
System.out.println("============================");
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nList = (NodeList)xPath.evaluate("/plist/dict/dict/dict/string", root, XPathConstants.NODESET);
for (int i = 0; i < nList.getLength(); ++i) {
Element e = (Element) nList.item(i);
String value = e.getFirstChild().getNodeValue();
logger.info("string:" + value);
}
公共类CustomXMLParse{
公共静态void main(字符串[]args){
试一试{
FileInputStream file=newfileinputstream(新文件(“/home/amitparashar/Documents/itunes3.xml”);
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=builderFactory.newDocumentBuilder();
Document xmlDocument=builder.parse(文件);
XPath=XPathFactory.newInstance().newXPath();
字符串表达式=“/plist/dict/dict/dict/String[1]”;
System.out.println(表达式);
NodeList NodeList=(NodeList)xPath.compile(expression.evaluate(xmlDocument,XPathConstants.NODESET);
for(int i=0;i
我想你是被那个.xml
文件卡住了吧?你不能改变吗?设计不是很好。不幸的是,我不能,我可以得到字符串值,但我想添加更多的标记,这样我可以提取更多的歌曲名称。如果我想得到一个特定的字符串,那么如果我在字符串标记中添加一个值,我将如何做到这一点?这只是常规的xpath语法。此XPath将获取具有“比阳光更明亮”的字符串/plist/dict/dict/dict/string[包含(,'Brighter')]
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(ResourceUtils.getFile("/itunes.xml").getAbsolutePath());
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());
System.out.println("============================");
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nList = (NodeList)xPath.evaluate("/plist/dict/dict/dict/string", root, XPathConstants.NODESET);
for (int i = 0; i < nList.getLength(); ++i) {
Element e = (Element) nList.item(i);
String value = e.getFirstChild().getNodeValue();
logger.info("string:" + value);
}
plist
============================
Nov 20, 2015 4:40:59 PM demo.TestCode testXml
INFO: string:Brighter Than Sunshine
Nov 20, 2015 4:41:00 PM demo.TestCode testXml
INFO: string:Aqualung
Nov 20, 2015 4:41:00 PM demo.TestCode testXml
INFO: string:Aqualung
public class CustomXMLParse {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("/home/amitparashar/Documents/itunes3.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/plist/dict/dict/dict/string[1]";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
} catch (Exception e) {
}
}
}