<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

            用Java如何處理XML數據

            時間:2024-10-22 07:43:52 JAVA認證 我要投稿
            • 相關推薦

            用Java如何處理XML數據

              Java原生內置的處理XML的技術基本有這么幾種:DOM,SAX,Stax,Jaxb.那么用Java我們要如何處理XML數據,希望對大家有幫助!

              DOM :Document Object Model 顧名思義就是在內存中構建樹形結構。處理小的XML文件還算勉強應付。如果文件比較大,他需要一次性裝載整個XML,你會忍不了他的速度,并且他會吃光你所有的內存,最后程序會負分滾粗。

              SAX:Simple API for XML Parsing 一般名字應該是沒實現的愿望體現。比如一個人如果叫王金庫,那么可以肯定他絕對沒有金庫。這樣你應該理解這個API為啥叫Simple了。這API是回調式的,也就是你寫的程序是被別人調戲用的。這API比DOM快點,而且是可以部分裝載XML,這樣你不用害怕OOME了。啥?你想寫XML?忘掉這個叫Simple的玩意兒吧。

              Stax: Streaming API for XML 這個總算是靠點譜了。你寫的程序是主動式的訪問XML各個節點。流式訪問,不用擔心OOME,速度嘛算是原生態里面最好的了。而且讀寫都支持,你不用這個還用哪個啊?

              給個Stax的代碼片段,算是參考吧:

              XMLInputFactory xif = XMLInputFactory.newInstance();

              XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));

              xsr.nextTag(); // Advance to statements element

              long i = 0;

              String action = null;

              while (xsr.hasNext()) {

              if (xsr.next() == XMLStreamConstants.START_ELEMENT) {

              if ("ContentItem".equals(xsr.getLocalName())) {

              action = getAttributeValue(xsr, "action");

              } else if ("Data".equals(xsr.getLocalName())) {

              i ++;

              }

              }

              }

              JAXB:Java Architecture for XML Binding 這是一個很酷的API.想法就是把XML各種屬性定義映射到普通的Java Bean上。你無須關心解析反解析的細節,只要用普通的Java Bean就完成和XML的交互了。可是怎么構建一一映射的JavaBean呢?在已知XML的定義的情況下你可以用自帶的xjc 命令自動生成相對應的JavaBean.如果你用Eclipse,有類似的插件幫你完成這步。具體可以google一下。然后你要做的就是僅僅是使用數據啦。簡單的令人發指:

              JAXBContext jc = JAXBContext.newInstance("com.your.xml.datatype.bean"); // 先注冊你的JavaBean

              // Create unmarshaller

              Unmarshaller um = jc.createUnmarshaller();

              // Unmarshal XML contents of the file myDoc.xml into your Java object

              // instance

              ObjectFactory objFac = new ObjectFactory(); // 生成Bean之后你會有這個工廠類的

              objFac.createYourData(objFac.createYourDataType());

              BufferedInputStream bis = new BufferedInputStream(new FileInputStream("your.xml")); // 你要怎么打開你的XML文件呢?

              JAXBElement myJAXBObject = (JAXBElement) um.unmarshal(bis); // 讀取

              YourDataType yourData = (YourDataType) myJAXBObject.getValue(); // 可以用啦

              // 下面是寫XML的例子

              Marshaller m = jc.createMarshaller();

              m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

              File outfile = new File("yourOutput.xml");

              BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outfile), 4096);

              m.marshal(myJAXBObject, bos); // 一步寫入。 myJAXBObject 需要你自己構建,你要存什么呢

              try {

              bos.flush();

              } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

              } finally {

              try {

              bos.close();

              } catch (Exception e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

              }

              }

              你也許意識到問題了:這一下子把XML Load到內存里,你是不是瘋了?為了避免瘋掉,你可以用Satx啊,那玩意兒不是流式的么?給個栗子,讀取這樣的XML文件片段:

              <OutXMLData>
              <SubXMLItemType>
              …
              </SubXMLItemType>
              <SubXMLItemType>
              …
              </SubXMLItemType>
              <SubXMLItemType>
              …
              </SubXMLItemType>
              …
              </OutXMLData>

              private static void readWriteWithStAXAndJAXB() throws FactoryConfigurationError, FileNotFoundException, XMLStreamException, UnsupportedEncodingException, JAXBException,

              PropertyException {

              // set up a StAX reader

              XMLInputFactory xmlif = XMLInputFactory.newInstance();

              BufferedInputStream bis = new BufferedInputStream(new FileInputStream("inputLarge.xml"));

              XMLStreamReader xmlr = xmlif.createXMLStreamReader(bis);

              File outfile = new File("output\outfile.xml");

              OutputStreamWriter bos = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8");

              XMLOutputFactory xmlof = XMLOutputFactory.newInstance();

              XMLStreamWriter xmlw = xmlof.createXMLStreamWriter(bos);

              xmlw.writeStartDocument("UTF-8", "1.0");

              xmlw.writeStartElement("OutXMLData");

              JAXBContext ucontext = JAXBContext.newInstance(SubXMLItemType.class);

              Unmarshaller unmarshaller = ucontext.createUnmarshaller();

              Marshaller marshaller = ucontext.createMarshaller();

              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

              marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

              xmlr.nextTag();

              xmlr.require(XMLStreamConstants.START_ELEMENT, null, "OutXMLData");

              xmlr.nextTag();

              int iCount = 0;

              while (xmlr.getEventType() == XMLStreamConstants.START_ELEMENT) { // 按標簽流式讀取

              iCount++;

              JAXBElement pt = unmarshaller.unmarshal(xmlr, SubXMLItemType.class); // 只讀取映射SubItem的內容

              marshaller.marshal(pt, xmlw); // 這步是分批流式寫入

              xmlw.writeCharacters(" ");

              if (xmlr.getEventType() == XMLStreamConstants.CHARACTERS) {

              xmlr.next();

              }

              }

              xmlw.flush();

              xmlw.writeEndElement();

              xmlw.writeEndDocument();

              System.out.println("Entity Count is :" + iCount);

              xmlr.close();

              xmlw.close();

              }

              說完這么多,基本上用Java處理XML已經不是難事了。不過,有時候你會有:給你蟹八件兒,你也無從下嘴的感受。比如,解析XML你可以掌控,隨便你用啥,可是你調用的下游程序接口卻需要另外一種格式的數據。比如,你用Stax解析XML,下游要DOM接口會不會令你抓狂起來?心里咒罵,倒霉玩意兒,你們還有沒有點上進心?!最近我就遇到這事了,解析一個大的XML,下游要Sub的XML,或者叫XML片段,或者叫分割XML文件。好么,我把數據都拆成Java的Object,然后再給你拼成一個個小的XML文件發過去,喪心病狂么這不?!你如果真這么做了,就別往下看了,你會哭的!

              Java 的XML包下面有個transform的子包,看看里面會有驚喜的。可以用這個工具包幫你完成類似的轉換,比如Stax 和 Sax 或者Dom 互相的變換。或者變換成Stream.

              拿我這個分割XML的小栗子來說:

              XMLInputFactory xif = XMLInputFactory.newInstance();

              XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); // 用Stax讀取XML

              xsr.nextTag(); // Advance to statements element

              TransformerFactory tf = TransformerFactory.newInstance();

              Transformer t = tf.newTransformer();

              t.setParameter(OutputKeys.OMIT_XML_DECLARATION, "no");

              t.setParameter(OutputKeys.STANDALONE, "yes");

              long i = 0;

              String action = null;

              while (xsr.hasNext()) {

              if (xsr.next() == XMLStreamConstants.START_ELEMENT) {

              if ("ContentItem".equals(xsr.getLocalName())) {

              action = getAttributeValue(xsr, "action");

              } else if ("Data".equals(xsr.getLocalName())) {

              File file = new File("out/" + action + i++ + ".xml");

              t.transform(new StAXSource(xsr), new StreamResult(file)); // 流式變換,走你~

              // DOMResult dr = new DOMResult(); // 如果你要Dom格式的,releaseMe

              // t.transform(new StAXSource(xsr), dr);

              }

              }

              }

              知道最變態的是什么嗎?需要解析XML整個內容到String里面,不單單是數據,就是整個XML標簽和數據。其實就是ouputStream轉String的過程:

              ByteArrayOutputStream baos = new ByteArrayOutputStream();

              t.transform(new StAXSource(xsr), new StreamResult(baos));

              String subXMLStr = baos.toString();

            【用Java如何處理XML數據】相關文章:

            關于XML技術在數據交換中的應用03-29

            Java中日期的處理方法03-09

            如何編譯java程序03-05

            如何讓JAVA代碼更高效03-20

            java數據類型和運算符03-06

            Excel如何橫向輸入數據03-03

            Java如何實現簡單的whois查詢03-16

            Java byte[]轉int如何實現03-16

            word表格中的數據如何排序02-21

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院