有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

错误:使用java解析xml

我试图读取xml文件并将其转换为html,我使用pdfbox创建xml,我创建一个xml文件,以便获得粗体、斜体、斜体或下划线的格式。行标记表示另一行

这是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>

这是我的Java代码

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());

共 (1) 个答案

  1. # 1 楼答案

    将第121行替换为:

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