Re: extracting part of xml



- Download the JDOM library from http://www.jdom.org.

- Import the library in your project / favorite IDE

Given the following an XML file called items.xml with the following
contents:
<?xml version="1.0"?>
<info>
<item>hello</item>
<item>world</item>
<item>!</item>
</info>

We will be producing 3 files, each named item1.xml, item2.xml,
item3.xml with the following piece of code using the JDOM library:

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
import java.util.*;

public class XMLItemManipulator {

private List<Element> items;

public XMLItemManipulator() {
items = null;
}

public void readItems(File xmlFile) throws FileNotFoundException,
IOException {

// make sure the file exists and can be read
if(!xmlFile.exists())
throw new FileNotFoundException("cannot find the xml file");

if(!xmlFile.canRead())
throw new IOException("file exists but does not have *read*
permission");

// now that we have made sure we got the file, just get the objects
// necessary to read it and create and XML doc outta if
SAXBuilder builder = new SAXBuilder();
Document doc = null;

try {
doc = builder.build(xmlFile);
} catch(JDOMException e) {
System.out.println("An error occured while build the XML Doc!");
e.printStackTrace();
}

// get the root element, in you case this would be <info>
Element root = doc.getRootElement();

// get the list of children of the root element
// which have the "item" tag.
// meaning that even if you had other tags that
// were children of the root, we really wouldn't care
// perfect for an heterogenous xml file containing more
// than the "item" elements
items = root.getChildren("item");
}


// now that you got the items you might want to manipulate them
// it depends on what you wanna do with them while they're in
// memory. I recommend you have a look at the JDOM doc for more info.
public void manipulateItems() {
// put some code here
}


// once you have manipulated them or since you got the items,
// you can now decide to write them separately to files.
// To do this, it's very simple.
public void writeItems() throws IOException, Exception {
Element root = null;
Document doc = null;
FileWriter writer = null;
XMLOutputter out = new XMLOutputter();
int size = items.size();

try {
for(int counter = 0; counter < size; counter++) {
root = new Element("item");
root.addContent(items.get(counter).cloneContent());

doc = new Document(root);
writer = new FileWriter(new File("item" + counter + ".xml"));
out.output(doc, writer);
out.output(doc, System.out);
}


} catch(IOException e) {
throw e; // put better handling of exception here
} catch(Exception e) {
throw e; // put better handling of exception here
} finally {
try {
writer.close();
} catch(Exception e) {
e.printStackTrace(); // imagine better handling here
}
}
}


// testing all of this with a main method (normally you'd write)
// a full test case to do this but that's your decision
public static void main(String[] args) {
XMLItemManipulator manip = new XMLItemManipulator();
File file = new File("items.xml");

try {
manip.readItems(file);
manip.manipulateItems(); // this is optional
manip.writeItems();

} catch(Exception e) {
e.printStackTrace();
}
}
}

There you go. Let us know how it goes.

Regards,

Jean-Paul H.

.



Relevant Pages

  • Re: Problem modifying XML view definitions from installed C# Add-in
    ... > attempts to modify an XML view definition for an Outlook folder (similar ... > It works fine on my development machine, but when I distribute the add-in ... > public void OnDisconnection(Extensibility.ext_DisconnectMode ...
    (microsoft.public.outlook.program_addins)
  • Re: extracting part of xml
    ... Given the following an XML file called items.xml with the following ... public void readItemsthrows FileNotFoundException, ... // get the list of children of the root element ... Jean-Paul H. ...
    (comp.lang.java.programmer)
  • Re: XML and Java from Beginner
    ... If it is then you should not be parsing the XML manual. ... public void charactersthrows SAXException { ... public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException { ...
    (comp.lang.java.programmer)
  • Re: User defined SAX Events
    ... startElement(), endElement() etc... ... mappings] public void endEmployee() ... XML is the programmer's duct tape. ...
    (comp.lang.java.programmer)
  • Re: XMLDOM, the System.Xml namespace and refreshing data
    ... > This kind of thing should work for the XML stuff. ... > public void GetData() { ... > public void Update() { ...
    (microsoft.public.dotnet.general)