1

I have this sample xml.

Each row has an id field, it has values as bits.

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.

This is the example code in java and 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());
        }

    }

}
rmtheis
  • 5,992
  • 12
  • 61
  • 78
user921131
  • 31
  • 1
  • 1
  • 5

1 Answers1

1

From looking at the XPATH reference there is no such thing as bitwise operations.

You could work around that though by making use of existing operations (mod etc).

See here for a related question.

EDIT:

Sample 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:

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. You could move the operation outside of XPATH and do it in Java as a workaround.

Community
  • 1
  • 1
Wivani
  • 2,036
  • 22
  • 28
  • I've reviewed the link but it isnt' usefull because I want to find without xsl. – user921131 Aug 31 '11 at 08:55
  • I understand the workaorund but the real xml has 52.000 items and it's very slow each query. I don't believe that it's not possible by xpath. – user921131 Aug 31 '11 at 13:05
  • And XPATH is your only possibility? An XML that large seems to be better of being parsed by SAX. – Wivani Aug 31 '11 at 13:21
  • Perhaps but I don't know so how could I find by SAX with BITAND ¿? – user921131 Sep 01 '11 at 07:24
  • It comes down to using the bitwise operators in Java if that's within your range of options. It's just that XML's this large are more performance-wise better handled by SAX than DOM. – Wivani Sep 01 '11 at 07:59