Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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 从.jsp格式的xml中提取内容_Java_Xml - Fatal编程技术网

Java 从.jsp格式的xml中提取内容

Java 从.jsp格式的xml中提取内容,java,xml,Java,Xml,监视器URL--http://host03:8810/solr/admin/stats.jsp其中包含此xml文件 <?xml-stylesheet type="text/xsl" href="stats.xsl"?> <solr> <core></core> <schema>test</schema> <host>domain.host.com</host> &

监视器URL--
http://host03:8810/solr/admin/stats.jsp
其中包含此xml文件

    <?xml-stylesheet type="text/xsl" href="stats.xsl"?>
<solr>

      <core></core> 

  <schema>test</schema>
  <host>domain.host.com</host>
  <now>Fri Nov 11 11:14:01 PST 2011</now>
  <start>Thu Sep 22 18:33:06 PDT 2011</start>
  <solr-info>

    <CORE>

    <entry>
      <name>
        core
      </name>
      <class>

      </class>
      <version>
        1.0
      </version>
      <description>
        SolrCore
      </description>
      <stats>

        <stat name="coreName" >

        </stat>

        <stat name="startTime" >
          Thu Sep 22 18:33:06 PDT 2011
        </stat>

        <stat name="refCount" >
          2
        </stat>

        <stat name="aliases" >
          []
        </stat>

      </stats>
    </entry>


    <entry>
      <name>
        searcher
      </name>
      <class>
        org.apache.solr.search.SolrIndexSearcher
      </class>
      <version>
        1.0
      </version>
      <description>
        index searcher
      </description>
      <stats>

        <stat name="searcherName" >
          Searcher@5b637a2d main
        </stat>

        <stat name="caching" >
          true
        </stat>

        <stat name="numDocs" >
          111959
        </stat>

        <stat name="maxDoc" >
          112310
        </stat>

        <stat name="reader" >
          DirectoryReader(segments_h0 _1zn:Cv101710/351 _1zl:Cv8026 _1zp:Cv2574)
        </stat>

        <stat name="readerDir" >
          org.apache.lucene.store.NIOFSDirectory@/es_idx_prd/projects/index/solr-agile/document/data/index lockFactory=org.apache.lucene.store.NativeFSLockFactory@2c164804
        </stat>

        <stat name="indexVersion" >
          1313979005459
        </stat>

        <stat name="openedAt" >
          Fri Nov 11 11:00:04 PST 2011
        </stat>

        <stat name="registeredAt" >
          Fri Nov 11 11:00:04 PST 2011
        </stat>

        <stat name="warmupTime" >
          0
        </stat>

      </stats>
    </entry>

     </solr-info>
</solr>
您可以使用Dom4J(或任何XML)和XPATH:

import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

public class XPathExample {

  public static void main(String[] args) 
   throws ParserConfigurationException, SAXException, 
          IOException, XPathExpressionException {

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true); // never forget this!
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse("books.xml");

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr 
     = xpath.compile("//numDocs");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue()); 
    }
  }
}
import java.io.IOException;
导入org.w3c.dom.*;
导入org.xml.sax.SAXException;
导入javax.xml.parsers.*;
导入javax.xml.xpath.*;
公共类XPathExample{
公共静态void main(字符串[]args)
抛出ParserConfiguration异常、SAXException、,
IOException,XPathExpressionException{
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);//永远不要忘记这一点!
DocumentBuilder=domFactory.newDocumentBuilder();
documentdoc=builder.parse(“books.xml”);
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression
=xpath.compile(“//numDocs”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i

要继续我的评论,如果XML/文本结构相同,那么您可以

 if(line.contains("numDocs")) {
     //Extract numDocs value- How to do this?
     String numDocs = in.readLine(); // May need trimming.
     System.out.println("Num docs:" + numDocs);   
 }

如果.jsp恰好有一个有效的XML,您可以解析它并使用XPath获取所需元素的值。任何基于我的代码的示例都将不胜感激。。!!您在此处发布的XML似乎不是有效的。您可以尝试在web浏览器中打开它(第一行被注释掉),然后查看失败的地方。
 if(line.contains("numDocs")) {
     //Extract numDocs value- How to do this?
     String numDocs = in.readLine(); // May need trimming.
     System.out.println("Num docs:" + numDocs);   
 }