我怎样才能用按位和xpath和java在xml中查找?

发布于 2024-12-02 12:15:10 字数 1469 浏览 2 评论 0原文

我有这个示例 xml

每行都有一个 id 字段,它的值是位。

我想在这个文件中使用按位与运算符查找,但我不知道这是否可能。

我读到了关于运算符“&”的内容在 Oracle 中的 javascript 或 comand BITAND 中,但在 xml o 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());
        }

    }

}

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());
        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动听の歌 2024-12-09 12:15:10

从 XPATH 参考来看,不存在按位运算之类的东西。

您可以通过利用现有操作(mod 等)来解决这个问题。

有关相关问题,请参阅此处

编辑:

示例 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 代码:

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);

    }

}

同样,XPATH 似乎没有按位运算。作为解决方法,您可以将操作移到 XPATH 之外并在 Java 中执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文