2016-11-29 62 views
1

我想讀取xml文件並將其轉換爲html,我使用pdfbox創建xml,我創建一個xml文件以獲得格式,如果它的粗體或斜體或傾斜或下劃線。該行標記,則意味着另一條線error:parse xml using java

這是XML

<parent> 
<row> 
    <text x="127.000000" y="56.348022" w="70.655991" h="10.784000" format="Myriad-Bold">Typefaces</text> 
    <text x="202.407990" y="56.348022" w="20.511993" h="10.784000" format="Myriad-Bold">for</text> 
    <text x="227.671982" y="56.348022" w="59.903976" h="10.784000" format="Myriad-Bold">Symbols</text> 
    <text x="292.327972" y="56.348022" w="13.760010" h="10.784000" format="Myriad-Bold">in</text> 
    <text x="310.839966" y="56.348022" w="65.615967" h="10.784000" format="Myriad-Bold">Scientific</text> 
    <text x="381.207916" y="56.348022" w="87.696014" h="10.784000" format="Myriad-Bold">Manuscripts</text> 
</row> 
<row> 
    <text x="55.776001" y="82.271973" w="20.167328" h="5.557680" format="Times-Roman">Most</text> 
    <text x="78.554527" y="82.271973" w="20.467804" h="5.557680" format="Times-Roman">word</text> 
    <text x="101.631851" y="82.271973" w="42.598907" h="5.557680" format="Times-Roman">processing</text> 
    <text x="146.840286" y="82.271973" w="33.981995" h="5.557680" format="Times-Roman">software</text> 
    <text x="183.433319" y="82.271973" w="16.684677" h="5.557680" format="Times-Roman">now</text> 
    <text x="202.725845" y="82.271973" w="7.748871" h="5.557680" format="Times-Roman">in</text> 
    <text x="213.084244" y="82.271973" w="13.275162" h="5.557680" format="Times-Roman">use</text> 
    <text x="228.970444" y="82.271973" w="7.189453" h="5.557680" format="Times-Roman">at</text> 
    <text x="238.771088" y="82.271973" w="21.511993" h="5.557680" format="Times-Roman">NIST</text> 
    <text x="262.894196" y="82.271973" w="6.336151" h="5.557680" format="Times-Roman">is</text> 
    <text x="271.838257" y="82.271973" w="29.798767" h="5.557680" format="Times-Roman">capable</text> 
    <text x="304.248077" y="82.271973" w="8.296783" h="5.557680" format="Times-Roman">of</text> 
    <text x="315.154297" y="82.271973" w="40.387817" h="5.557680" format="Times-Roman">producing</text> 
    <text x="358.151611" y="82.271973" w="34.539764" h="5.557680" format="Times-Roman">lightface</text> 
    <text x="395.302429" y="82.271973" w="18.255005" h="5.557680" format="Times-Roman">(that</text> 
    <text x="416.168610" y="82.271973" w="8.824554" h="5.557680" format="Times-Roman">is,</text> 
    <text x="427.602692" y="82.271973" w="31.523499" h="5.557680" format="Times-Roman">regular)</text> 
    <text x="461.735626" y="82.271973" w="8.296783" h="5.557680" format="Times-Roman">or</text> 
    <text x="472.641846" y="82.271973" w="33.673218" h="5.557680" format="Times-Roman">boldface</text> 
    <text x="508.926117" y="82.271973" w="24.035065" h="5.557680" format="Times-Roman">letters</text> 
    <text x="535.569092" y="82.271973" w="8.296753" h="5.557680" format="Times-Roman">of</text> 
</row> 
</parent> 

,這裏是我的javacode

File fXmlFile = new File("1.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(fXmlFile); 
      doc.getDocumentElement().normalize(); 
      NodeList nList = doc.getElementsByTagName("row"); 
      for (int temp = 0; temp < nList.getLength(); temp++) { 
       org.w3c.dom.Node nNode = nList.item(temp); 
       if (nNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 

        Element eElement = (Element) nNode; 
        for(int i=0; i < eElement.getChildNodes().getLength(); i++){ 
         System.out.println("Content: " + eElement.getElementsByTagName("text").item(i).getTextContent()); 
        } 
       } 
      } 

,這裏是錯誤

Caused by: java.lang.NullPointerException 
    at com.trb.resizablerect.AppController.initialize(AppController.java:121) 

,這裏是行121

System.out.println("Content: " + eElement.getElementsByTagName("text").item(i).getTextContent()); 

回答

3

替換線121:

  NodeList nodeList = eElement.getElementsByTagName("text"); 
      if(nodeList!= null && nodeList.getLength() > i){ 
       Node node = nodeList.item(i); 
       System.out.println("Content: " + node.getTextContent()); 
      } 
+0

謝謝你,它的工作,跟進的問題,我如何獲取文本標籤的特定屬性?謝謝 –

+1

請[**接受**](http://meta.stackoverflow.com/q/5234/234215)這個答案,併爲您的額外問題創建一個新的職位。謝謝。 – kjhughes

+0

好的後續問題解決了: 我接受你的答案作爲解決方案,謝謝 if(node.hasAttributes())NamedNodeMap nodeMap = node.getAttributes(); System.out.println(nodeMap.getNamedItem(「format」)); } –