aspose file tools*
The moose likes XML and Related Technologies and the fly likes Urgent help !!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Urgent help !!!!!!" Watch "Urgent help !!!!!!" New topic
Author

Urgent help !!!!!!

vivek sivakumar
Ranch Hand

Joined: Aug 09, 2001
Posts: 187
Response: :
<?xml version="1.0" encoding="ISO-8859-1"?>
<XArC version="2.0">
<document>
<docid>100000</docid>
<metainfo name ='Datum' value ='05.06.2000'></metainfo>
<metainfo name ='Format' value ='CIB_XML'></metainfo>
<metainfo name ='Kontonr' value ='2003617103'></metainfo>
<metainfo name ='Persnr' value ='95897543'></metainfo>
<appinfo name ='accessed' value =''></appinfo>
<appinfo name ='archived' value =''></appinfo>
</document>
<document>
<docid>100009</docid>
<metainfo name ='Datum' value ='05.06.2000'></metainfo>
<metainfo name ='Format' value ='CIB_XML'></metainfo>
<metainfo name ='Kontonr' value =''></metainfo>
<metainfo name ='Persnr' value ='95897543'></metainfo>
<appinfo name ='accessed' value =''></appinfo>
<appinfo name ='archived' value =''></appinfo>
</document>
<document>
<docid>100015</docid>
<metainfo name ='Datum' value ='05.06.2000'>
...
this is a response im getting from a server , what i want to do is that i want to extract the value from "<docid>....</docid> tag , how can i do it from my java program ..im a new bee to this kindly help me ...or some ideas would be greatly appreciated..
[ April 20, 2002: Message edited by: Mapraputa Is ]
[ April 20, 2002: Message edited by: vivek sivakumar ]

SCJP, SCWD <br />A farmer learns more from a bad harvest than a good one.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
There are several ways to do it, listed from the most complex to the simpliest:
1. To use a standard DOM or SAX compliant parser. This is probably the most complex way.
2. There is a simplier version of parser - JDOM.
3. You can use "binding" packages, such as JAXB or Castor, which will read DTD or Schema for your XML document, and generate Java classes to work with it. After it, you will be able to access your XML elements in terms of Java collections, instead of Node, Element, or whatever. Here is a very good article that shows how to generate Java classes out of XML and how to use them:
http://www.javaworld.com/javaworld/jw-12-2001/jw-1228-jaxb.html
If neither DTD nor Schema as defined for your XML, you can generate one based on an instance document with XML Spy (free evaluation copy available). Here is DTD for your example, which you can try to feed to JAXB
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT XArC (document+)>
<!ATTLIST XarC
version CDATA #REQUIRED
>
<!ELEMENT appinfo EMPTY>
<!ATTLIST appinfo
name (accessed | archived) #REQUIRED
value CDATA #REQUIRED
>
<!ELEMENT docid (#PCDATA)>
<!ELEMENT document (docid, metainfo+, appinfo+)>
<!ELEMENT metainfo EMPTY>
<!ATTLIST metainfo
name CDATA #REQUIRED
value CDATA #REQUIRED
>
Which way you should choose depends on how much XML work you will need to do. If all you need is to read one this particular XML, the solution #3 looks the best.
vivek sivakumar
Ranch Hand

Joined: Aug 09, 2001
Posts: 187
Thanks a lot for your reply , actually what happens is that i just get a reply from a server and this reply is in the above stated form(my xml file) and i need to parse the incoming response automatically to get my <docid> values , might be some times my reply from the server might not have any <docid> at all ..... please advice on this further.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Actually, binding (JAXB/Castor) will leave you with a bunch of classes which you do not need.
JDOM is easier.Here is an article about how to use it.
All you need is to 1) download JDOM, 2) add its jar file to your classpath, 3) tweak "servlet" example from the article ("Using JDOM to read a web.xml file" section) - its XML structure is similar to your. Just change "servlet" to "document" and use getChild("docid") method.

[ April 20, 2002: Message edited by: Mapraputa Is ]
vivek sivakumar
Ranch Hand

Joined: Aug 09, 2001
Posts: 187
<?xml version="1.0" encoding="ISO-8859-1"?><XArC version="2.0" service="GetHitList" type= "response"><folder name='Vermoegensstatus'><document><docid>100000</docid><metainfo name ='Datum' value ='05.06.2000'></metainfo><metainfo name ='Format' value ='CIB_XML'></metainfo><metainfo name ='Kontonr' value ='2003617103'></metainfo><metainfo name ='Persnr' value ='95897543'></metainfo><appinfo name ='accessed' value =''></appinfo><appinfo name ='archived' value =''></appinfo></document><document><docid>100009</docid><metainfo name ='Datum' value ='05.06.2000'></metainfo><metainfo name ='Format' value ='CIB_XML'></metainfo><metainfo name ='Kontonr' value =''></metainfo><metainfo name ='Persnr' value ='95897543'></metainfo><appinfo name ='accessed' value =''></appinfo><appinfo name ='archived' value =''></appinfo></document><document><docid>100015</docid><metainfo name ='Datum' value ='05.06.2000'></metainfo><metainfo name ='Format' value ='CIB_XML'></metainfo><metainfo name ='Kontonr' value ='2003617103'></metainfo><metainfo name ='Persnr' value ='95897543'></metainfo><appinfo name ='accessed' value =''></appinfo><appinfo name ='archived' value =''></appinfo></document></folder></XArC>
This is the program im using to get the value of child ("docid") from my xml but its giving me a null value someone help me how to do it ..pls
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.util.*;
/**
* <p><code>WarReader</code> demonstrates how to
* read a Servlet 2.2 Web Archive file with JDOM.
* </p>
*
* @author Brett McLaughlin, Jason Hunter
* @version 1.0
*/
public class WarReader {
/** Default SAX Driver class to use */
private static final String DEFAULT_SAX_DRIVER_CLASS =
"org.apache.xerces.parsers.SAXParser";
/** SAX Driver Class to use */
private String saxDriverClass;
/** <code>{@link SAXBuilder}</code> instance to use */
private SAXBuilder builder;
public static String filename ="D:/web.xml";
/**
* <p>
* This will create an instance of <code>{@link SAXBuilder}</code>
* for use in the rest of this program.
* </p>
*
* @param saxDriverClass <code>String</code> name of driver class to use.
*/
public WarReader(String saxDriverClass) {
this.saxDriverClass = saxDriverClass;
builder = new SAXBuilder(saxDriverClass);
}
/**
* <p>
* This will parse the specified WAR using SAX and the
* SAX driver class specified in the constructor.
* </p>
*
* @param filename <code>String</code> name of file to parse.
* @param out <code>OutputStream</code> to output to.
*/
public void read(String request, PrintStream out)
throws IOException, JDOMException {
// Build the JDOM Document
Document doc = builder.build(new File(filename));
// Get the root element
Element root = doc.getRootElement();
System.out.println(root.getChildren());
List document = root.getChildren();
System.out.println(document);
List docu = root.getAttributes();
System.out.println(docu);
List servlets = root.getChildren("document");
System.out.println(servlets);
//String text = docid.getText();

//System.out.println(docid);
Element XArC = doc.getRootElement();
System.out.println(XArC);

}

public static void main(String[] args) {
PrintStream out = System.out;
if (args.length != 1 && args.length != 2) {
out.println("Usage: org.jdom.examples.servlet.WarReader [web.xml]");
return;
}
// Load filename and SAX driver class
String filename = args[0];
String saxDriverClass = DEFAULT_SAX_DRIVER_CLASS;
if (args.length == 2) {
saxDriverClass = args[1];
}
// Create an instance of the tester and test
try {
WarReader reader = new WarReader(saxDriverClass);
reader.read(filename, System.out);
} catch (JDOMException e) {
if (e.getRootCause() != null) {
e.getRootCause().printStackTrace();
} else {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Hey, now your document got <folder> element between the root and all <document>s ! How fixed your structure is? Because getChildren() method will depend on the structure. If your XML structure is not fixed (tags can be added or omitted), then you will have to use more flexible algorithm. There is only one folder element in your file, is it always so?
Where do you have null pointer exception, at which line? Your code has Xerces parser call, do you have Xerces installed on your machine and accessible via classpath? If not, you can download it from here: http://xml.apache.org/xerces-j/index.html
This simplified version works on my machine, you can try it and after you make it work add exceptions checks, pretty printing etc.
vivek sivakumar
Ranch Hand

Joined: Aug 09, 2001
Posts: 187
hi Mapraputa ,
thanks a TON for ur extended support.. man it was really good of u..yesterday was sunday ..still i receive answers for my queries... i sometimes wonder abt u guys ..whether u have any kind of week ends...
 
wood burning stoves
 
subject: Urgent help !!!!!!