Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed Help to read text file
    primarykey
    data
    text
    <p>i am a new in JAVA and i want to read text file and write it in XML here is my input:</p> <ol> <li>R.-J. Roe, J. Appl.Phys. 36, 2024 (1965).</li> </ol> <p>and the output but is:</p> <pre><code> &lt;ref id="1"&gt; &lt;label&gt;1&lt;/label&gt; &lt;citation-alternatives&gt; &lt;mixed-citation&gt;R.-J. Roe, J. Appl.Phys. 36, 2024 (1965).&lt;/mixed-citation&gt; &lt;/citation-alternatives&gt; &lt;/ref&gt; </code></pre> <p>put in many cases this input comes in two lines without space between them like this:</p> <ol> <li><p>R.-J. Roe,</p> <p>J. Appl.Phys. 36, 2024 (1965).</p></li> </ol> <p>and the output will be this:</p> <pre><code> &lt;ref id="1"&gt; &lt;label&gt;1&lt;/label&gt; &lt;citation-alternatives&gt; &lt;mixed-citation&gt;R.-J. Roe, &lt;/mixed-citation&gt; &lt;/citation-alternatives&gt; &lt;/ref&gt; &lt;ref id="1"&gt; &lt;label&gt;1&lt;/label&gt; &lt;citation-alternatives&gt; &lt;mixed-citation&gt;J. Appl.Phys. 36, 2024 (1965).&lt;/mixed-citation&gt; &lt;/citation-alternatives&gt; &lt;/ref&gt; </code></pre> <p>Now my question is how can i read this two lines as one stirng to be like the first output? and here is my code:</p> <pre><code>try { String strLine; String num=""; String mix=""; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // Back element Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("Back"); doc.appendChild(rootElement); // ref-list element Element reflist = doc.createElement("ref-list"); rootElement.appendChild(reflist); while( (strLine = br.readLine()) != null) { if (strLine.equals("")) { continue; } int dotIndex = strLine.indexOf("."); num = strLine.substring(0,dotIndex); mix = strLine.substring(dotIndex+2,strLine.length()); // ref element Element ref= doc.createElement("ref"); reflist.appendChild(ref); // set attribute of ref element Attr attr = doc.createAttribute("id"); attr.setValue(num); ref.setAttributeNode(attr); // label element Element label = doc.createElement("label"); ref.appendChild(label); label.setTextContent(num); // citation-alternatives element Element citationalternatives = doc.createElement("citation-alternatives"); ref.appendChild(citationalternatives); // mixed-citation element Element mixedcitation = doc.createElement("mixed-citation"); citationalternatives.appendChild(mixedcitation); mixedcitation.setTextContent(mix); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload