xml dom解析器在java中按名称查找标记
我有一个xml: 我只是想得到城市和温度,我试过这个:xml dom解析器在java中按名称查找标记,java,xml,parsing,dom,Java,Xml,Parsing,Dom,我有一个xml: 我只是想得到城市和温度,我试过这个: HttpParams httpParameters = new BasicHttpParams(); HttpClient httpclient = new DefaultHttpClient(httpParameters); HttpGet httpget = new HttpGethttp://netmobileag.accu weather.com/widget/netmobileag/weather-data.asp?slat=
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGethttp://netmobileag.accu weather.com/widget/netmobileag/weather-data.asp?slat=48.04960&slon=21.71420&metric=1");
HttpResponse response = httpclient.execute(httpget);
InputStream in = response.getEntity().getContent();
String res = "";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(in));
doc.getDocumentElement().normalize();
NodeList loc = doc.getElementsByTagName("local");
for (int i = 0; i < loc.item(0).getChildNodes().getLength(); i++) {
res = res + "\n" + loc.item(0).getChildNodes().item(0).getNodeValue();
}
摘要25x。如何获取城市和温度值?尝试getTextContent()而不是getNodeValue()尝试以下代码
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGethttp://netmobileag.accu weather.com/widget/netmobileag/weather-data.asp?slat=48.04960&slon=21.71420&metric=1");
HttpResponse response = httpclient.execute(httpget);
InputStream in = response.getEntity().getContent();
String res = "";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(in));
doc.getDocumentElement().normalize();
NodeList loc = doc.getElementsByTagName("local");
for (int i = 0; i < loc.item(0).getChildNodes().getLength(); i++) {
res = res + "\n" + loc.item(0).getChildNodes().item(0).getTextContent();
}
HttpParams httpParameters=new BasicHttpParams();
HttpClient HttpClient=新的默认HttpClient(httpParameters);
HttpGet HttpGet=新的HttpGethttp://netmobileag.accu weather.com/widget/netmobileag/weather data.asp?slat=48.04960&slon=21.71420&metric=1“;
HttpResponse response=httpclient.execute(httpget);
InputStream in=response.getEntity().getContent();
字符串res=“”;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新输入源(in));
doc.getDocumentElement().normalize();
NodeList loc=doc.getElementsByTagName(“本地”);
对于(int i=0;i
正确的做法是实际使用Xpath。是否使用正则表达式或SQL在数据库中搜索?同样,在XML文件中搜索也是通过Xpath完成的
package xpath;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class SimpleParsing {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("/Users/eugenrabii/Desktop/MyFile.xml");
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression xPathExpression = xpath.compile("//city/text()");
Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET);
System.out.println(result.toString());
NodeList nodes = (NodeList) result;
System.out.println(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
packagexpath;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathConstants;
导入javax.xml.xpath.XPathExpression;
导入javax.xml.xpath.XPathFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
公共类简单化{
公共静态void main(字符串[]args)引发异常{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);//永远不要忘记这一点!
DocumentBuilder=factory.newDocumentBuilder();
Document doc=builder.parse(“/Users/eugenrabii/Desktop/MyFile.xml”);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
XPathExpression=xpath.compile(“//city/text()”);
Object result=xPathExpression.evaluate(doc,XPathConstants.NODESET);
System.out.println(result.toString());
节点列表节点=(节点列表)结果;
System.out.println(nodes.getLength());
对于(int i=0;i
}@Robertoq你的意思是什么?我只是猜测XML结构可能会改变?你能提供你测试的XML文件吗?
package xpath;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class SimpleParsing {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("/Users/eugenrabii/Desktop/MyFile.xml");
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression xPathExpression = xpath.compile("//city/text()");
Object result = xPathExpression.evaluate(doc, XPathConstants.NODESET);
System.out.println(result.toString());
NodeList nodes = (NodeList) result;
System.out.println(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}