Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取xml节点?_Java_Android - Fatal编程技术网

Java 如何获取xml节点?

Java 如何获取xml节点?,java,android,Java,Android,下面是我的xml,这将在设备的文件夹中,我将读取xml文件。如何使用XmlPullParser在android中获取/读取或返回xml节点 <?xml version="1.0" encoding="utf-8"?> <appLayouts> <AIRLINE_FORM sendSelectionEvent="false"> <RESULT multiSelect="false"></RESULT> &l

下面是我的xml,这将在设备的文件夹中,我将读取xml文件。如何使用XmlPullParser在android中获取/读取或返回xml节点

<?xml version="1.0" encoding="utf-8"?>
<appLayouts>
    <AIRLINE_FORM sendSelectionEvent="false">
        <RESULT multiSelect="false"></RESULT>
    </AIRLINE_FORM>
    <ALLOWANCES_FORM sendTableEditEvent="false" sendSelectionEvent="true">
        <RESULT multiSelect="false">
            <field header="Category" size="75" align="left" edit="false"></field>
            <field header="Allowance" size="-1" align="left" edit="false"></field>
       </RESULT>
    </ALLOWANCES_FORM>
</appLayouts>

您需要传递要访问的每个节点的节点名。下面是帮助您获取节点的代码

public String getValue(Element item, String str) {      
    NodeList n = item.getElementsByTagName(str);        
    return this.getElementValue(n.item(0));
}

public final String getElementValue( Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
  } 

如果可以的话,我建议您使用
DOM4J
xml解析器。解析xml数据后,访问节点可以非常简单:

xml.selectSingleNode("./AIRLINE_FORM");
这样可以避免
XMLPullParser
所需的迭代

这是我使用
DOM4J
的工作代码:

1) 创建文件解析器:

public class XMLUtil {
    public static Node parseFile(InputStream is) {
        Node retData = null;
        SAXReader xmlReader = new SAXReader();
        try {
            Document document = xmlReader.read(is);
            retData = document.getRootElement();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return retData;
    }
}
2) 只需指定节点的路径即可提取所需内容(示例用法:
getNodeFromPath(“./AIRLINE\u FORM”)

请注意,“/AIRLINE_FORM”中的“.”是您的根标记(“Applayout”)

干杯

xml.selectSingleNode("./AIRLINE_FORM");
public class XMLUtil {
    public static Node parseFile(InputStream is) {
        Node retData = null;
        SAXReader xmlReader = new SAXReader();
        try {
            Document document = xmlReader.read(is);
            retData = document.getRootElement();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return retData;
    }
}
private Node getNodeFromPath(String path) {
    Node retData = null;
    InputStream is = null;

    try {
        is = getAssets().open("filename.xml");
        Node xml = XMLUtil.parseFile(is);
        retData = xml.selectSingleNode(path);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            if(is != null) is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return retData;
}