Skip to content

Commit

Permalink
skip XML declarations at the begining
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamer Gur committed Jun 15, 2019
1 parent 1141265 commit 1b306d7
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 1 deletion.
9 changes: 9 additions & 0 deletions sample.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Comment at top before declarations -->
<!DOCTYPE note SYSTEM "Note.dtd">
<!DOCTYPE chapter [
<!ELEMENT chapter (title,para+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT para (#PCDATA)>
]>
<!-- Comment at top after declarations -->
<examples>
<tag1 att1="<att0>" att2="att0">
<tag11 att1="att0">InnerText110</tag11>
Expand Down
134 changes: 133 additions & 1 deletion xmlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (x *XMLParser) parse() {
var b byte
var iscomment bool

err = x.skipDeclerations()

if err != nil {
x.sendError()
return
}

for {
b, err = x.readByte()

Expand Down Expand Up @@ -336,7 +343,6 @@ search_close_tag:

func (x *XMLParser) isComment() (bool, error) {

x.scratch.reset()
var c byte
var err error

Expand All @@ -351,6 +357,27 @@ func (x *XMLParser) isComment() (bool, error) {
return false, nil
}

var d, e byte

d, err = x.readByte()

if err != nil {
return false, err
}

e, err = x.readByte()

if err != nil {
return false, err
}

if d != '-' || e != '-' {
err = x.defaultError()
return false, err
}

// skip part
x.scratch.reset()
for {

c, err = x.readByte()
Expand All @@ -369,6 +396,111 @@ func (x *XMLParser) isComment() (bool, error) {

}

func (x *XMLParser) skipDeclerations() error {

var err error
var a, c, d byte
var b []byte

scan_declartions:
for {
a, err = x.readByte()

if err != nil {
return err
}

if x.isWS(a) {
continue
}

if a == '<' {

b, err = x.reader.Peek(1) // this is needed because we cant unread 2 bytes consequtively

if err != nil {
return err
}

if b[0] == '!' || b[0] == '?' { // either comment or decleration

_, err = x.readByte()

if err != nil {
return err
}

c, err = x.readByte()

if err != nil {
return err
}

d, err = x.readByte()

if err != nil {
return err
}

if c == '-' && d == '-' {
goto skipComment
} else {
goto skipDecleration
}

} else { // declerations ends.

err = x.unreadByte()
return err

}

}
}

skipComment:
x.scratch.reset()
for {

c, err = x.readByte()

if err != nil {
return err
}

if c == '>' && len(x.scratch.bytes()) > 1 && x.scratch.bytes()[len(x.scratch.bytes())-1] == '-' && x.scratch.bytes()[len(x.scratch.bytes())-2] == '-' {
goto scan_declartions
}

x.scratch.add(c)

}

skipDecleration:
depth := 1
for {

c, err = x.readByte()

if err != nil {
return err
}

if c == '>' {
depth--
if depth == 0 {
goto scan_declartions
}
continue
}
if c == '<' {
depth++
}

}

}

func (x *XMLParser) closeTagName() (string, error) {

x.scratch.reset()
Expand Down

0 comments on commit 1b306d7

Please sign in to comment.