Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Stupid Newbie Question

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: tcdfemgdnf AT snkmail.com
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] Stupid Newbie Question
  • Date: Fri, 13 Mar 2015 21:26:28 -0700

Greetings all,

Sorry if this is a stupid question but I'm new to XML and trying to figure out how to export a XML file from my Java app.
I'm using XOM-1.2.10 and Java JRE 1.8.0_31-b13. My program (not production quality) writes the attached stuff.xml which I believe looks reasonable, but when I try to read it, line 73 /int kids = record.getChildCount();///sets kids to 9. I don't understand why since I attach 4 elements to the "record" element.

BTW, I just added the serial number to the "record" element because I was seeing unexpected behavior. I'd like to remove it and have just
<file ...>
<record>
<field id="1stfieldName">1stvalue</field>
<field id="2ndfieldName">2nd field value</field>
<field id="3rdfieldName">3rdfield value</field>
<record>
<record>
<field id="1stfieldName">1stvalue</field>
<field id="2ndfieldName">2nd field value</field>
<field id="3rdfieldName">3rdfield value</field>
<record>
</file>

Am I on the right track or do I need to learn a lot more about XML before I try this in the real world?

TIA for whatever advice I can get.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import nu.xom.Attribute;
import nu.xom.Attribute.Type;
import nu.xom.Builder;
import nu.xom.Comment;
import nu.xom.DocType;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.Serializer;

/**
*
* @author mag
*/
public class Main {

File xmlFile;
private int recNum = 0;

public Main() {
xmlFile = new File("stuff.xml");
write();
try {
read();
}
catch (ParsingException | IOException xx) {
xx.printStackTrace(System.err);
}
}

private void read() throws ParsingException, IOException {
System.out.format("%s %s\n", xmlFile.getAbsolutePath(),
xmlFile.exists());
FileInputStream is = new FileInputStream(xmlFile);

int indent = 0;
String nam = null;
String val = null;
java.util.Stack<String> stack = new java.util.Stack<>();

Builder builder = new Builder();
Document doc = builder.build(is);


Element root = doc.getRootElement();
System.out.format("<%s", root.getLocalName());
for (int inx = 0; inx < root.getAttributeCount(); inx++) {
System.out.format(" %s=\"%s\"",
root.getAttribute(inx).getLocalName(),

root.getAttribute(inx).getValue());
}
System.out.println(">");

recNum = 0;

Elements records = root.getChildElements(String.format("record%08d",
++recNum));
int rcdIdxCnt = records.size();
for (int rcdIdx = 0; rcdIdx < rcdIdxCnt; rcdIdx++) {
Element record = records.get(rcdIdx);
stack.push(record.getLocalName());
System.out.format("%s<%s", indent(++indent), stack.peek());

// No attributes for record
String atr = "";
int atrCnt = record.getAttributeCount();
for (int atrInx = 0; atrInx < atrCnt; atrInx++) {
Attribute attribute = record.getAttribute(atrInx);
nam = attribute.getLocalName().trim();
val = attribute.getValue().trim();
atr += String.format(" %s=\"%s\"", nam, val);
}
int kids = record.getChildCount();
// kids is now set to 9
System.out.format(">%s</%s>\n", record.getValue(), stack.empty()
? "?" : stack.pop()); // <record>

// for (int rcdIdxInx = 0; rcdIdxInx < record.getChildCount();
rcdIdxInx++) {
// Elements fields = record.getChildElements();
// ++indent;
// int fldCnt = fields.size();
// for (int fldInx = 0; fldInx < fields.size(); fldInx++) {
// Element field = fields.get(fldInx);
// System.out.format("%s<%s", indent(indent),
field.getLocalName());
// atrCnt = field.getAttributeCount();
// for (int atrInx = 0; atrInx < atrCnt; atrInx++) {
// Attribute attr = field.getAttribute(atrInx);
// nam = attr.getLocalName();
// val = attr.getValue();
// System.out.format(" %s=\"%s\"", nam, val);
// }
// System.out.println(">");
// }
// --indent;
// }
}
// ++indent;


is.close();
}

private String indent(int units) {
StringBuilder sb = new StringBuilder();
for (int inx = 0; inx < units; inx++) {
sb.append(" ");
}
return sb.toString();
}

private void write() {
Element file = new Element("file");
Document doc = new Document(file);
file.addAttribute(new Attribute("file","someFile"));
file.addAttribute(new Attribute("version","m.n"));
file.addAttribute(new Attribute("date","mm/dd/yyyy_hh:mm:ss"));

file.appendChild(newRecord(new Data("1", "Fred", "40.00", " ")));
file.appendChild(newRecord(new Data("12", "Bob", "23.50", "Fred")));
// file.appendChild(newRecord(new Data("13", "Jenny", "22.75",
"Bob")));
// file.appendChild(newRecord(new Data("14", "Bill", "23.00", "Bob")));
// file.appendChild(newRecord(new Data("15", "Becky", "19.00",
"Fred")));
// file.appendChild(newRecord(new Data("15", "Chuck", "20.00",
"Fred")));

try {
FileOutputStream os = new FileOutputStream(xmlFile);
Serializer ser = new Serializer(os);
ser.setIndent(4);
ser.setMaxLength(64);

ser.write(doc);

os.close();
}
catch (IOException xx) {
xx.printStackTrace(System.err);
}
}

private Element newRecord(Data data) {
Element record = new Element(String.format("record%08d", ++recNum));
Element field = new Element("field");
field.addAttribute(new Attribute("id", "key"));
field.appendChild(data.key);
record.appendChild(field);

field = new Element("field");
field.addAttribute(new Attribute("id", "name"));
field.appendChild(data.name);
record.appendChild(field);

field = new Element("field");
field.addAttribute(new Attribute("id", "rate"));
field.appendChild(data.rate);
record.appendChild(field);

field = new Element("field");
field.addAttribute(new Attribute("id", "mgr"));
field.appendChild(data.mgr);
record.appendChild(field);

return record;
}

public static void main(String[] args) {
Main m = new Main();
}

class Data {
String key;
String name;
String rate;
String mgr;

public Data(String key, String name, String rate, String mgr) {
this.key = key;
this.name = name;
this.rate = rate;
this.mgr = mgr;
}
}
}



Archive powered by MHonArc 2.6.24.

Top of Page