1

i'm trying to read the xml file and convert it to html, i used pdfbox to create the xml, i create an xml file in order to get the format if its bold or italic or slant or underline. the row tag means another line

this is the 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>

and here is my 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());
                    }
                }
            }

and here is the error

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

and here is the line 121

System.out.println("Content: " + eElement.getElementsByTagName("text").item(i).getTextContent());
pdf to image
  • 369
  • 6
  • 23

1 Answers1

3

Replace line 121 with:

            NodeList nodeList = eElement.getElementsByTagName("text");
            if(nodeList!= null && nodeList.getLength() > i){
                Node node = nodeList.item(i);
                System.out.println("Content: " + node.getTextContent());
            }
Vitaly Olegovitch
  • 3,509
  • 6
  • 33
  • 49
  • thank you it worked, follow up question, how do i get the specific attribute of the text tag? thank you – pdf to image Nov 29 '16 at 02:15
  • 1
    Please [**accept**](http://meta.stackoverflow.com/q/5234/234215) this answer and a create new post for your additional question. Thanks. – kjhughes Nov 29 '16 at 02:21
  • Ok follow up question solved: And i accept your answer as the solution, thank you if (node.hasAttributes()){ NamedNodeMap nodeMap = node.getAttributes(); System.out.println(nodeMap.getNamedItem("format")); } – pdf to image Nov 29 '16 at 02:27