.java中怎样获取xml中节点属性,即:下文 aa 的值 5
//.java中怎样获取xml中节点属性,即:下文aa的值//xml结构test.xml<?xmlversion="1.0"encoding="utf-8"?><prod...
// .java中怎样获取xml中节点属性,即:下文 aa 的值
// xml结构 test.xml
<?xml version="1.0" encoding="utf-8"?>
<productData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<itemData>
<height aa="MM">
<value>275</value>
</height>
</itemData>
</productData>
java 代码
public class Parserxml{
public static void main(String []args) throws ParserConfigurationException, SAXException, IOException {
getDataFromXml("test.xml");
}
public static void getDataFromXml(String xmlpath) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlpath);
doc.normalize();
NodeList links = doc.getElementsByTagName("productData");
Element itemLink=(Element) links.item(0).getChildNodes().item(1);
String str = itemLink.getElementsByTagName("height").item(0).getChildNodes().item(1).getTextContent();
System.out.println("str --> "+str);//
}
} 展开
// xml结构 test.xml
<?xml version="1.0" encoding="utf-8"?>
<productData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<itemData>
<height aa="MM">
<value>275</value>
</height>
</itemData>
</productData>
java 代码
public class Parserxml{
public static void main(String []args) throws ParserConfigurationException, SAXException, IOException {
getDataFromXml("test.xml");
}
public static void getDataFromXml(String xmlpath) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlpath);
doc.normalize();
NodeList links = doc.getElementsByTagName("productData");
Element itemLink=(Element) links.item(0).getChildNodes().item(1);
String str = itemLink.getElementsByTagName("height").item(0).getChildNodes().item(1).getTextContent();
System.out.println("str --> "+str);//
}
} 展开
2个回答
展开全部
直接上代码了
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Parserxml {
public static void main(String[] args) throws Exception{
getDataFromXml("NewFile.xml");
}
public static void getDataFromXml(String xmlpath)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlpath);
doc.normalize();
NodeList links = doc.getElementsByTagName("productData");
Element itemLink = (Element) links.item(0).getChildNodes().item(1);
String str = itemLink.getElementsByTagName("height").item(0).getAttributes().getNamedItem("aa").getNodeValue();
System.out.println("str --> " + str);//
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-29
展开全部
晕死,楼上竟然用dom解析牛b啊,
追问
请教更简洁有效的方法,谢谢~
追答
简单的肯定是用dom4j或者jdom啊,毕竟公司差不多都用这解析,如果楼主不懂这个的话还是用dom解析或者sax..初学者还是应该懂这个的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询