Java 使用XPath搜索XML

Java 使用XPath搜索XML,java,android,xml,xpath,xml-parsing,Java,Android,Xml,Xpath,Xml Parsing,我在URL上存储了一个XML。我正在解析来自Android应用程序的URL。让URL为 我想使用XPath遍历一些节点并显示结果。但是当我阅读教程时,我知道XML文件必须存储在项目中。我的意思是这样的: Document document = builder.parse(new File("/books.xml")); 如果我给出XML文件的URL,即,而不是先保存文件然后使用它,XPath是否可以工作 编辑:以下是代码: public class MainActivity extends

我在URL上存储了一个XML。我正在解析来自Android应用程序的URL。让URL为

我想使用XPath遍历一些节点并显示结果。但是当我阅读教程时,我知道XML文件必须存储在项目中。我的意思是这样的:

Document document = builder.parse(new File("/books.xml"));
如果我给出XML文件的URL,即,而不是先保存文件然后使用它,XPath是否可以工作

编辑:以下是代码:

public class MainActivity extends ListActivity {
// data
ArrayList<String> mPeople = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        parseData();
    } catch(Exception ex) {
        Toast.makeText(this, "Exception: " + ex.getMessage(), Toast.LENGTH_LONG).show();
    }

    // pass adapter w/ data queried through XPath to ListView
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPeople);
    setListAdapter(adapter);
}

private void parseData() throws Exception {
    // create an InputSource object from /res/raw

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new URL("http://www.w3schools.com/xpath/books.xml").openConnection().getInputStream());
       doc.getDocumentElement().normalize();
    // query XPath instance, this is the parser
    XPath xpath = XPathFactory.newInstance().newXPath();
    // specify the xpath expression
    String expression = " /bookstore/book/title ";
    // list of nodes queried
    NodeList nodes = (NodeList)xpath.evaluate(expression, doc, XPathConstants.NODESET);

    Toast.makeText(this, "count: " + String.valueOf(nodes.getLength()),Toast.LENGTH_SHORT).show();
    // if node found
    if(nodes != null && nodes.getLength() > 0) {
        mPeople.clear();
        int len = nodes.getLength();
        for(int i = 0; i < len; ++i) {
            // query value
            Node node = nodes.item(i);
            mPeople.add(node.getTextContent());
        }
    }
}
public类MainActivity扩展了ListActivity{
//资料
ArrayList mPeople=新的ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
试一试{
解析数据();
}捕获(例外情况除外){
Toast.makeText(这个,“异常:+ex.getMessage(),Toast.LENGTH_LONG.show();
}
//将适配器和通过XPath查询的数据传递给ListView
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,mPeople);
setListAdapter(适配器);
}
私有void parseData()引发异常{
//从/res/raw创建InputSource对象
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新URL(“http://www.w3schools.com/xpath/books.xml)。openConnection().getInputStream());
doc.getDocumentElement().normalize();
//查询XPath实例,这是解析器
XPath=XPathFactory.newInstance().newXPath();
//指定xpath表达式
字符串表达式=“/bookstore/book/title”;
//查询的节点列表
NodeList节点=(NodeList)xpath.evaluate(表达式、文档、XPathConstants.NODESET);
Toast.makeText(这个,“count:+String.valueOf(nodes.getLength()),Toast.LENGTH_SHORT.show();
//如果找到节点
if(nodes!=null&&nodes.getLength()>0){
mPeople.clear();
int len=nodes.getLength();
对于(int i=0;i

任何帮助都将不胜感激。

使用
新URL(yourURL).openConnection().openStream()
并将该流传递给生成器解析方法。您的意思是?
Document Document=builder.parse(新URL(“myURL”).openConnection.openStream();
@SotiriosDelimanolis它说:
方法openStream()是的,我的错,它是
getInputStream()
,而不是
openStream()
。什么都没有发生!日志猫说:
打开跟踪文件时出错。没有这样的文件或目录(2)