Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

java - Traversing an XML and Adding Elements

We are currently trying to remove XMLBeans from our project and trying to get everything to JAXB. While migrating through I came across something that XMLBeans provided natively called XMLCursor API. Which traversed through an XML and added elements wherever a user felt like. Is there a way to get this done in JAXB or any other XML API? I was researching XMLStreamReader API, which is pretty close but it doesn't have any method to write to an XML. I am guessing I might have to combine, XMLStreamReader and XMLStreamWriter. But cant figure out how to. The latter API doesn't store any location information. Just for some perspective, I have added the old code just let you know what I mean.

// If child elements, insert after last child
if (cur.toLastChild()) {

    cur.toEndToken();
    cur.toNextToken();
    cur.insertElementWithText(elementName, nameSpace, elementValue);
    
} else {
            
    // Else insert here
    cur.toEndToken();
    cur.insertElementWithText(elementName, nameSpace, elementValue);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...