extract data from XML
Hello,
if i have xml like this:
<Elements>
<Element id="a1">
<Name>One</Name>
<Description>Blablabla</Description>
</Element>
<Element id="a2">
<Name>Two</Name>
<Description>Blablabla</Description>
</Element>
..................................
<Element id="a1000000">
<Name>Million</Name>
<Description>Blablabla</Description>
</Element>
</Elements>
and parse it to DOM, and want to get element by name. The solution is to go through all the elements and chech if the field name have the desired value. I gues there is a faster way? Does anyone know? What about XPath?
[803 byte] By [
ramazas1] at [2007-9-27 1:59:12]

By "faster" do you mean "requires less time to write the program" or "requires less time to run the program"? XPath would probably require less time to write the program, if you got it right (XPath can be tricky to work with). It might run in less time, too, but you would have to try it with your real data to find that out.
Hi! I think I can't help u so much but I've seen your topic
and I want to ask u if u can show some basic code for a simple
extraction of a XML document with a parser.
I'll be so thankful cause I'm finishing my career and I have to make
some extractions from an XML document and I don't know how
(I'm starting in the world of XML),
Please, help me!
First of all you must have a parser, I use Sun's JAXP.
Then you need to parse your XML file into DOM.
org.w3c.dom.Node domNode;
Document document;
public void LoadYourXML() {
document=null;
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
try {
document = builder.parse(new File("your.xml"));
} catch (FileNotFoundException fnf) {}
domNode = document;
}
catch (SAXException sxe) {}
catch (ParserConfigurationException pce) {}
catch (IOException ioe) {System.out.println(}
}
catch (Exception ex) {}
}
And know you have domNode object (which is like a tree), and you walk though it and extract data you need.
for example:
domNode.getNodeName();
domNode.getChildNodes().item(0).getNodeName()
domNode.getNodeValue() and so on.
By "faster" i mean that a method which returns Element Id by Name value, will return it faster, then going throug all the elements.
If I have parsed XML into DOM already nto objects:
org.w3c.dom.Node domNode;
Document document;
I can use method : public NodeList getElementsByTagName(java.lang.String tagname)
NodeList nl = document.getElementsByTagName("Million");
int i=0;
while (true) {
if (nl.item(i).getChildNodes().item(0).getNodeValue().compareTo("Million")==0) {break;}
i++;
}
But i guess it will be too slow if I have a lot of elements.
I think i will have to find out what's XPath is for.
Thanks again.
> By "faster" do you mean "requires less time to write
> the program" or "requires less time to run the
> program"? XPath would probably require less time to
> write the program, if you got it right (XPath can be
> tricky to work with). It might run in less time, too,
> but you would have to try it with your real data to
> find that out.
I'm trying the code above but not getting what I expect. Here's what I have:
org.w3c.dom.Node domNode = null;
Document document = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
try {
document = builder.parse(new File("C:\\TEMP\\testfile.xml"));
}catch (FileNotFoundException fnf) {
System.out.println("a");
}
domNode = document;
}catch (SAXException sxe) {
System.out.println("b");
}catch (ParserConfigurationException pce) {
System.out.println("c");
}catch (IOException ioe) {
System.out.println("d");
}catch (Exception ex) {
System.out.println("e");
}
}catch (Exception e) {
System.out.println("f");
}
try {
System.out.println("Node name: " +domNode.getNodeName());
System.out.println(domNode.getChildNodes().item(0).getNodeName());
}catch(Exception e) {
}
Here's my output:
Node name: #document
RECORD
RECORD is the name of my first node. However, when I change the name of the node in the XML file to "REC", the output still says "RECORD". This makes me think that it is not actually seeing my file. Any ideas why this is happening? I am sure that I am saving my changes to the XML file. Please help.
My ultimate goal is to extract data from the XML file so I can assign the values to my variables.
