Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Append in XML file

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Wolfgang Hoschek <wolfgang.hoschek AT mac.com>
  • To: Narjis Malik <narjismalikster AT gmail.com>
  • Cc: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] Append in XML file
  • Date: Fri, 2 Mar 2007 11:15:21 -0800

Attachments don't seem to work on this mailing list. So here is the program attached inline...


package nux.xom.sandbox;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Comment;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.NodeFactory;
import nu.xom.Nodes;
import nu.xom.ProcessingInstruction;
import nu.xom.Text;
import nux.xom.io.StreamingSerializer;
import nux.xom.io.StreamingSerializerFactory;
import nux.xom.pool.XOMUtil;

/**
* Efficiently appends an element to the end of an XML file, in a streaming manner.
*
* @author whoschek.AT.lbl.DOT.gov
*/
public class AppendDemo {

public static void main(String[] args) throws Exception {
OutputStream out = new FileOutputStream("/tmp/result.xml");
StreamingSerializer serializer = new StreamingSerializerFactory ().createXMLSerializer(out, "UTF-8");
NodeFactory streamingNodeFactory = XOMUtil.getRedirectingNodeFactory (serializer);
NodeFactory appendingNodeFactory = new AppendingNodeFactory (streamingNodeFactory);
new Builder(appendingNodeFactory).build(new File(args[0])); // stream through

// check results:
System.out.println(new Builder().build(new File("/tmp/ result.xml")).toXML());
}


private static final class AppendingNodeFactory extends NodeFactory {

private final NodeFactory child;

public AppendingNodeFactory(NodeFactory child) {
this.child = child;
}

// replace this with whatever tree you want to see appended...
private Element createNodeToAppend() {
Element synonym = new Element("synonymx");
synonym.addAttribute(new Attribute("name", "foo"));
return synonym;
}

public Nodes finishMakingElement(Element element) {
if (element.getParent() instanceof Document) {

print(createNodeToAppend());
child.makeText("\n\n");
}
return child.finishMakingElement(element);
}

private void print(Element elem) {
child.startMakingElement(elem.getQualifiedName(), elem.getNamespaceURI());
for (int i=0; i < elem.getAttributeCount(); i++) {
Attribute attr = elem.getAttribute(i);
child.makeAttribute(attr.getQualifiedName(), attr.getNamespaceURI(), attr.getValue(), attr.getType());
}
for (int i=0; i < elem.getChildCount(); i++) {
Node node = elem.getChild(i);
if (node instanceof Element) {
print((Element)node); // recurse
} else if (node instanceof Text) {
child.makeText(node.getValue());
} else if (node instanceof Comment) {
child.makeComment(node.getValue());
} else if (node instanceof ProcessingInstruction) {
ProcessingInstruction pi =
(ProcessingInstruction) node;

child.makeProcessingInstruction(pi.getTarget(), pi.getValue());
}
}
child.finishMakingElement(elem);
}

// remaining methods simply delegate through to child...
public Element makeRootElement(String name, String namespace) {
return child.makeRootElement(name, namespace);
}

public Element startMakingElement(String name, String namespace) {
return child.startMakingElement(name, namespace);
}

public Document startMakingDocument() {
return child.startMakingDocument();
}

public void finishMakingDocument(Document document) {
child.finishMakingDocument(document);
}

public Nodes makeAttribute(String name, String URI, String value, Attribute.Type type) {
return child.makeAttribute(name, URI, value, type);
}

public Nodes makeComment(String data) {
return child.makeComment(data);
}

public Nodes makeDocType(String rootElementName, String publicID, String systemID) {
return child.makeDocType(rootElementName, publicID, systemID);
}

public Nodes makeText(String data) {
return child.makeText(data);
}

public Nodes makeProcessingInstruction(String target, String data) {
return child.makeProcessingInstruction(target, data);
}

}
}



On Mar 2, 2007, at 11:13 AM, Wolfgang Hoschek wrote:

If efficiency is not a concern, the XPath approaches outlined by Grzegorz will be easy. A perhaps more syntactically convenient alternative is to use the XQueryUtil.update() method. Both have the same effect.

However, if efficiency in terms of time and space is important, and alternative approach via a streaming NodeFactory is needed, finding the insertion point as the input document streams by, and appending new elements to an output xml stream. I've attached a small program that does the append in such a streaming manner. You might want to adapt it for your purposes.

But if efficiency really matters this is still not good enough because with file based XML, each separate append still has to scan the entire file to find the end, so appending many elements separately will be O(N^2). You'll end up having to consider better alternatives such as a database or B-trees such as embedded JDBM.

Wolfgang.


On Mar 2, 2007, at 1:58 AM, Narjis Malik wrote:

Hello,


I am working on XML files using nux and xPath, I want to append xml
doucument at a particular location, how can I do it....

I will demonstrate my question with the following example

i have a xml document
_____________________________________________________________________ ______________

<?xml version="1.0" encoding="UTF-8"?>

<service name="GetFutureSybmolization">

<synonym name="bvcvcvc">

</synonym>

</service>

_____________________________________________________________________ ________________

now what i need , i want to append this xml file with new element :

as i scan this xml and when there is service element with name =
"GetFutureSybolization"

then i want to add new element <synonym name="XYZDGD"></synonym>

please help me how can i do it...

as i tried to do it but it append at the end of file but i cant append in
some where else



waiting for a reply
_______________________________________________
XOM-interest mailing list
XOM-interest AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest

_______________________________________________
XOM-interest mailing list
XOM-interest AT lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest





Archive powered by MHonArc 2.6.24.

Top of Page