Added *document.LoadUri() method to load documents from the web (over http)

This commit is contained in:
jim teeuwen 2009-11-25 02:50:06 +01:00
parent 5bbe4dbe10
commit ccb91f470e
3 changed files with 35 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import "io"
import "strings"
import "xml"
import "fmt"
import "http"
type Document struct {
Version string;
@ -207,6 +208,23 @@ func (this *Document) LoadFile(path string) (err os.Error) {
return;
}
func (this *Document) LoadUri(uri string) (err os.Error) {
r, _, err := http.Get(uri);
if err != nil {
return
}
defer r.Body.Close();
b, err := io.ReadAll(r.Body);
if err != nil {
return
}
err = this.LoadString(string(b));
return;
}
func (this *Document) LoadStream(r *io.Reader) (err os.Error) {
content := "";
buff := make([]byte, 256);

View File

@ -4,6 +4,7 @@ import "os"
import "io"
type ILoader interface {
LoadUrl(string) os.Error;
LoadFile(string) os.Error;
LoadString(string) os.Error;
LoadStream(*io.Reader) os.Error;

View File

@ -2,7 +2,7 @@ package xmlx
import "testing"
func TestDoc(t *testing.T) {
func TestLoadLocal(t *testing.T) {
doc := New();
err := doc.LoadFile("test.xml");
@ -17,6 +17,21 @@ func TestDoc(t *testing.T) {
}
}
func TestLoadRemote(t *testing.T) {
doc := New();
err := doc.LoadUri("http://tldp.org/authors/template/Sample-HOWTO.xml");
if err != nil {
t.Errorf("%s", err);
return;
}
if len(doc.Root.Children) == 0 {
t.Errorf("Root node has no children.");
return;
}
}
func TestSave(t *testing.T) {
doc := New();
err := doc.LoadFile("test.xml");