简体   繁体   English

如何使用按位-和xpath和Java在xml中查找?

[英]How can I do to find in xml with bitwise-and with xpath and java?

I have this sample xml . 我有这个示例xml

Each row has an id field, it has values as bits. 每行都有一个id字段,它的值以位为单位。

And I want to find in this file with bitwise-and operator but I don't know if this is possible. 我想使用按位和运算符在此文件中查找,但是我不知道这是否可行。

I read about the operator '&' in javascript or comand BITAND in Oracle but nothing in xml o xpath. 我在Javascript或Oracle中用comand BITAND读取了运算符'&',但在xml o xpath中则没有。

This is the example code in java and xpath: 这是java和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.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Util implements java.io.Serializable  {

    static public String filter_xpath_bitand (int var_tipo)

        NodeList nodeList = null;
        Element  element  = null;
        try {
            XPath xpath = XPathFactory.newInstance().newXPath();
            DocumentBuilderFactory factory  = DocumentBuilderFactory.newInstance();
            DocumentBuilder        builder  = factory.newDocumentBuilder();
            Document            document = builder.parse(new File(fileXML));      
           nodeList = (NodeList)xpath.evaluate("/test/row[(id & \""+ var_tipo +"\") > 1]", document.getDocumentElement(), XPathConstants.NODESET);
        } catch (Exception e) {
            System.out.println("*** filterXML --> exception: " + e.toString());
        }

    }

}

From looking at the XPATH reference there is no such thing as bitwise operations. 从XPATH参考来看,没有像按位操作这样的东西。

You could work around that though by making use of existing operations (mod etc). 您可以通过使用现有操作(mod等)来解决此问题。

See here for a related question. 有关相关问题,请参见此处

EDIT: 编辑:

Sample xml: 样本XML:

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <row>
        <id>32</id>
        <titulo>yellow</titulo>
    </row>
    <row>
        <id>16</id>
        <titulo>green</titulo>
    </row>
    <row>
        <id>8</id>
        <titulo>red</titulo>
    </row>
    <row>
        <id>1</id>
        <titulo>blue</titulo>
    </row>
    <row>
        <id>2</id>
        <titulo>white</titulo>
    </row>
    <row>
        <id>4</id>
        <titulo>black</titulo>
    </row>
</test>

Java code: Java代码:

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;

import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class BitWiseXPathTest {

    public static void main(String[] args) {

        Set<String> selectedColors = new HashSet<String>();
        int var_tipo = 33;
        try {
            XPath xpath = XPathFactory.newInstance().newXPath();
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            String fileXML = "bitwise.xml";
            Document document = builder.parse(new File(fileXML));

            String evalStr = "/test/row/id";
            NodeList nodeList = (NodeList)xpath.evaluate(evalStr, document.getDocumentElement(), XPathConstants.NODESET);

            for (int i = 0; i < nodeList.getLength(); i++) {
                Node aNode = nodeList.item(i);
                if( (Integer.parseInt(aNode.getTextContent()) & var_tipo) > 0) {
                    //System.out.println("color: "+aNode.getNextSibling().getNextSibling().getTextContent());
                    selectedColors.add(aNode.getNextSibling().getNextSibling().getTextContent());
                }
            }

        } catch (Exception e) {
            System.out.println("*** filterXML --> exception: " + e.toString());
        }

        System.out.println(selectedColors);

    }

}

Again, XPATH doesn't seem to have a bitwise operation. 同样,XPATH似乎没有按位操作。 You could move the operation outside of XPATH and do it in Java as a workaround. 您可以将操作移到XPATH之外,并在Java中作为解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM