Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
如何将xml文件嵌入java包并访问它?_Java_Xml_Package - Fatal编程技术网

如何将xml文件嵌入java包并访问它?

如何将xml文件嵌入java包并访问它?,java,xml,package,Java,Xml,Package,我有一个XML文件,其中包含的数据在我的C#和Java版本的库中都使用。 理想情况下,我希望将此XML文件嵌入该库中的包中 我只需要从我的库中访问它,所以我想知道:这可能吗?在Java中,可以将XML文件本身包含在JAR文件中。然后,您可以使用如下内容: InputStream istream = getClass().getResourceAsStream("/resource/path/to/some.xml"); 并将您的InputStream正常解析 以上内容在当前类路径中查找,其中将

我有一个XML文件,其中包含的数据在我的C#和Java版本的库中都使用。 理想情况下,我希望将此XML文件嵌入该库中的包中


我只需要从我的库中访问它,所以我想知道:这可能吗?

在Java中,可以将XML文件本身包含在JAR文件中。然后,您可以使用如下内容:

InputStream istream = getClass().getResourceAsStream("/resource/path/to/some.xml");
并将您的
InputStream
正常解析

以上内容在当前类路径中查找,其中将包含任何JAR文件的内容。

book.xml
book.xml
<book>
<person>
  <first>Kiran</first>
  <last>Pai</last>
  <age>22</age>
</person>
<person>
  <first>Bill</first>
  <last>Gates</last>
  <age>46</age>
</person>
<person>
  <first>Steve</first>
  <last>Jobs</last>
  <age>40</age>
</person>
<person>
  <first>kunal</first>
  <last>kumar</last>
  <age>25</age>
</person>
</book>

create a xml file book.xml
made a jar file book.xml.jar and 
palce it in war/web-inf/lib folder of your project..
 then it will work..
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;




import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

@SuppressWarnings("serial")
public class XMLParser extends HttpServlet {
    InputStream istream =getClass().getResourceAsStream("/book.xml");
    public void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException
    {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = null;
        try {
            docBuilder = docBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      Document doc = null;
    try {
        doc = docBuilder.parse (istream);
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        // normalize text representation
        doc.getDocumentElement ().normalize ();
        System.out.println ("Root element of the doc is " + 
             doc.getDocumentElement().getNodeName());


        NodeList listOfPersons = doc.getElementsByTagName("person");
        int totalPersons = listOfPersons.getLength();
        System.out.println("Total no of people : " + totalPersons);

        for(int s=0; s<listOfPersons.getLength() ; s++){


            Node firstPersonNode = listOfPersons.item(s);
            if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                Element firstPersonElement = (Element)firstPersonNode;

                //-------
                NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                Element firstNameElement = (Element)firstNameList.item(0);

                NodeList textFNList = firstNameElement.getChildNodes();
                System.out.println("First Name : " + 
                       ((Node)textFNList.item(0)).getNodeValue().trim());

                //-------
                NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                Element lastNameElement = (Element)lastNameList.item(0);

                NodeList textLNList = lastNameElement.getChildNodes();
                System.out.println("Last Name : " + 
                       ((Node)textLNList.item(0)).getNodeValue().trim());

                //----
                NodeList ageList = firstPersonElement.getElementsByTagName("age");
                Element ageElement = (Element)ageList.item(0);

                NodeList textAgeList = ageElement.getChildNodes();
                System.out.println("Age : " + 
                       ((Node)textAgeList.item(0)).getNodeValue().trim());

                //------


            }//end of if clause


        }//end of for loop with s var

    //System.exit (0);

}//end of main


    }
基兰 派 22 比尔 盖茨 46 史蒂夫 乔布斯 40 库纳尔 库玛 25 创建一个xml文件book.xml 制作了一个jar文件book.xml.jar并 将其放在项目的war/web inf/lib文件夹中。。 那就行了。。 导入java.io.File; 导入java.io.IOException; 导入java.io.InputStream; 导入javax.servlet.http.*; 导入javax.xml.parsers.DocumentBuilder; 导入javax.xml.parsers.DocumentBuilderFactory; 导入javax.xml.parsers.parserConfiguration异常; 导入org.w3c.dom.Document; 导入org.w3c.dom.Element; 导入org.w3c.dom.Node; 导入org.w3c.dom.NodeList; 导入org.xml.sax.SAXException; 导入org.xml.sax.SAXParseException; @抑制警告(“串行”) 公共类XMLParser扩展了HttpServlet{ InputStream istream=getClass().getResourceAsStream(“/book.xml”); public void doGet(HttpServletRequest-req、HttpServletResponse-resp)引发IOException { DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder=null; 试一试{ docBuilder=docBuilderFactory.newDocumentBuilder(); }捕获(ParserConfiguration异常e){ //TODO自动生成的捕捉块 e、 printStackTrace(); } 单据单据=空; 试一试{ doc=docBuilder.parse(istream); }捕获(SAXE异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } //规范化文本表示 doc.getDocumentElement().normalize(); System.out.println(“文档的根元素为”+ doc.getDocumentElement().getNodeName()); NodeList listOfPersons=doc.getElementsByTagName(“个人”); int totalPersons=listOfPersons.getLength(); System.out.println(“总人数:“+totalPersons”);
对于(ints=0;sth),这在jarred导出和项目引用中都非常有效!