You are not logged in.

vpr

Beginner

  • "vpr" is male
  • "vpr" started this thread

Posts: 30

Date of registration: May 21st 2014

  • Send private message

1

Monday, June 23rd 2014, 6:12pm

Translating XML Parser Example from Java

Hi people :)

I've been translating this example of simple XML parsing from JAVA to Jabaco's syntax:

--------------------------------------------------------------------

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DOMExampleJava {

public static void main(String args[]) {
try {

File stocks = new File("Stocks.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();

System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("stock");
System.out.println("==========================");

for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Stock Symbol: " + getValue("symbol", element));
System.out.println("Stock Price: " + getValue("price", element));
System.out.println("Stock Quantity: " + getValue("quantity", element));
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
}



--------------------------------------------------------------------


It's taken from here:

http://javarevisited.blogspot.co.uk/2011…e-tutorial.html

They only problem I have is in knowing how to refrormat the lines:

Jabaco Source

1
Element element = (Element) node;


and

Jabaco Source

1
Node node = (Node) nodes.item(0);


How would these be written in Jabaco's syntax?

Thanks for any help :)

thunder

Beginner

Posts: 3

Date of registration: Jul 17th 2014

  • Send private message

2

Thursday, July 17th 2014, 9:33am

Hi could you please post your code cause I need help making an xml parser

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

3

Thursday, July 17th 2014, 6:30pm

Oh, have forgotten to answer to this post.

Create on your desktop the file "Stocks.xml" with this text in the file:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
 <?xml version="1.0" encoding="UTF-8"?>
<stocks>
       <stock>
              <symbol>Citibank</symbol>
              <price>100</price>
              <quantity>1000</quantity>
       </stock>
       <stock>
              <symbol>Axis bank</symbol>
              <price>90</price>
              <quantity>2000</quantity>
       </stock>
</stocks>

Then this Jabaco-port works:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Import java#io#File
Import javax#xml#parsers#DocumentBuilder
Import javax#xml#parsers#DocumentBuilderFactory
Import org#w3c#dom#Document
Import org#w3c#dom#Element
Import org#w3c#dom#Node
Import org#w3c#dom#NodeList

Public Sub main(ByJava args() As String)
  Dim stocks As New File("Stocks.xml")
  Dim dbFactory As DocumentBuilderFactory = DocumentBuilderFactory.newInstance
  Dim dBuilder As DocumentBuilder = dbFactory.newDocumentBuilder
  Dim doc As Document = dBuilder.parse(stocks)
  doc.getDocumentElement.normalize
  
  System.out.println("root of xml file " & doc.getDocumentElement.getNodeName)
  Dim nodes As NodeList = doc.getElementsByTagName("stock")
  System.out.println("==========================")
  
  Dim i As Integer
  For i = 0 To nodes.getLength -1
    Dim jnode As org#w3c#dom#Node = nodes.item(i)
  
    If jnode.getNodeType = jnode.ELEMENT_NODE Then
      Dim jelement As Element = Cast(jnode, Element)
      System.out.println("Stock Symbol: " & getValue("symbol", jelement))
      System.out.println("Stock Price: " & getValue("price", jelement))
      System.out.println("Stock Quantity: " & getValue("quantity", jelement))
    End If
  Next i

MsgBox ""
End Sub

Private Function getValue(tag As String, jelement As Element) As String
  Dim nodes As NodeList = jelement.getElementsByTagName(tag).item(0).getChildNodes
  Dim zNode As org#w3c#dom#Node = nodes.item(0)
  Dim jnode As org#w3c#dom#Node = Cast(zNode, org#w3c#dom#Node)
  getValue = jnode.getNodeValue
End Function

Greatings
theuserbl

thunder

Beginner

Posts: 3

Date of registration: Jul 17th 2014

  • Send private message

4

Saturday, July 26th 2014, 9:39am

Thank you!!!!!

thunder

Beginner

Posts: 3

Date of registration: Jul 17th 2014

  • Send private message

5

Tuesday, July 29th 2014, 12:03pm

Oh, have forgotten to answer to this post.
Do you have code to change a value in the xml file?

vpr

Beginner

  • "vpr" is male
  • "vpr" started this thread

Posts: 30

Date of registration: May 21st 2014

  • Send private message

6

Tuesday, July 29th 2014, 11:38pm

Thanks very much for this :) Will try it out as soon as I get the opportunity.

Rate this thread
WoltLab Burning Board