Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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_Xml_Parsing - Fatal编程技术网

Java XML搜索与解析

Java XML搜索与解析,java,xml,parsing,Java,Xml,Parsing,我有一个XML文件,我正试图用Java搜索它。我只需要通过标记名找到元素,然后找到该标记的值。例如: 我有一个XML文件: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="https://company.com/test/xslt/processing_report.xslt"?> <Certificate xmlns="urn:us:net:exchange

我有一个XML文件,我正试图用Java搜索它。我只需要通过标记名找到元素,然后找到该标记的值。例如:

我有一个XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://company.com/test/xslt/processing_report.xslt"?>
<Certificate xmlns="urn:us:net:exchangenetwork:Company">
  <Value1>Veggie</Value1>
  <Value2>Fruits</Value2>
    <type1>Apple</type1>
       <FindME>Red</FindME>
  <Value3>Bread</Value3>
</Certificate>

素食者
水果
苹果
红色
面包

我想在FindME标记中找到值。我不能使用XPath,因为不同的文件可以有不同的结构,但它们总是有一个FindME标记。最后,我在寻找最简单的代码,我不太关心性能。谢谢

我认为您需要仔细阅读XPath,因为它可以很容易地解决这个问题。在DOM API中使用getElementsByTagName也是如此。

我认为您需要了解XPath,因为它可以很容易地解决这个问题。在DOM API中使用getElementsByTagName也是如此。

您仍然可以使用XPath。您只需使用//FindMe()表达式即可。这将从xml中的任何位置查找“FindMe”元素,而不考虑其父元素或根元素的路径


如果您使用的是名称空间,那么请确保解析器知道您仍然可以使用XPath。您只需使用//FindMe()表达式即可。这将从xml中的任何位置查找“FindMe”元素,而不考虑其父元素或根元素的路径

如果您使用的是名称空间,请确保让解析器知道这一点

以下是代码:

    XPathFactory f = XPathFactory.newInstance();
    XPathExpression expr = f.newXPath().compile(
            "//*[local-name() = 'FindME']/text()");
    DocumentBuilderFactory domFactory = DocumentBuilderFactory
            .newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("src/test.xml"); //your XML file

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    System.out.println(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }
XPathFactory f=XPathFactory.newInstance();
XPathExpression expr=f.newXPath().compile(
“//*[local-name()='FindME']/text()”;
DocumentBuilderFactory domFactory=DocumentBuilderFactory
.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder=domFactory.newDocumentBuilder();
documentdoc=builder.parse(“src/test.xml”)//您的XML文件
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
System.out.println(nodes.getLength());
对于(int i=0;i
解释:

/*
-匹配任何元素节点-无论它们位于何处

local-name();不是完整路径-匹配“FindME”

text()
-获取节点值。

以下是代码:

    XPathFactory f = XPathFactory.newInstance();
    XPathExpression expr = f.newXPath().compile(
            "//*[local-name() = 'FindME']/text()");
    DocumentBuilderFactory domFactory = DocumentBuilderFactory
            .newInstance();
    domFactory.setNamespaceAware(true);
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("src/test.xml"); //your XML file

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    System.out.println(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }
XPathFactory f=XPathFactory.newInstance();
XPathExpression expr=f.newXPath().compile(
“//*[local-name()='FindME']/text()”;
DocumentBuilderFactory domFactory=DocumentBuilderFactory
.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder=domFactory.newDocumentBuilder();
documentdoc=builder.parse(“src/test.xml”)//您的XML文件
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
System.out.println(nodes.getLength());
对于(int i=0;i
解释:

/*
-匹配任何元素节点-无论它们位于何处

local-name();不是完整路径-匹配“FindME”


text()
-获取节点值。

你能给我举个例子吗?我试过各种各样的解析器。哦。。我认为名称空间可能是问题所在。如何让解析器知道这一点?
/*[local-name()='FindMe']
将不考虑名称空间此表达式将获取本地名称等于'FindMe'()的所有节点您能给我举个例子吗?我试过各种各样的解析器。哦。。我认为名称空间可能是问题所在。如何让解析器知道这一点?
/*[local-name()='FindMe']
将不考虑名称空间此表达式将获取本地名称等于'FindMe'()