1

all the examples about parsing xml elements/nodes, that i've found, are about how to extract node attributes/values etc from xml document.

Im interested on how should i extract an entire element from opening tag to closing tag. Example: from xml document

<?xml version="1.0"?>
    <Employees>
        <Employee emplid="1111" type="admin"/>
    </Employees>

i would get the complete element

<Employee emplid="1111" type="admin"/>

to saving it in a String variable

Thanks in advance

FUGAZI
  • 231
  • 1
  • 4
  • 10
  • 1
    Perhaps you should take a look at [this](http://stackoverflow.com/questions/9886531/how-to-parse-xml-with-jsoup) which uses [Jsoup](http://jsoup.org/). – Josh M Oct 27 '13 at 22:56
  • Yes, it works perfect, but is there something to make it without external libraries? Thanks a lot – FUGAZI Oct 27 '13 at 23:32
  • I've noticed that the method doc.select() (in http://stackoverflow.com/questions/9886531/how-to-parse-xml-with-jsoup) isn't key sensitive. If the xml element attributes name has some upper case characters, the returned string has lowercase :( I'm very sorry for this. – FUGAZI Oct 28 '13 at 00:16

2 Answers2

0

You can either do the parsing yourself, or use Android's XML parser. This shows how to use the latter.

If you use Android's parser you probably have to parse it completely, and then construct the string <Employee emplid="1111" type="admin"/> by hand.

Jeffrey Klardie
  • 3,020
  • 1
  • 18
  • 23
  • Hi, thanks for your answer. Constructing the string is axactly what i don't want make. I have to take the string from a parser that extract to me the full element. It's safe for future modifies to the xml element's attributes. EDIT: P.S. i've just made the contructing way, but doesn't like to my boss :( – FUGAZI Oct 27 '13 at 23:41
0

For now this is the best solution.

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

...

//xml document
String  xml = "<?xml version=\"1.0\"?><Employees><Employee emplid=\"1111\" type=\"admin\"/></Employees>";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(new ByteArrayInputStream(xml.getBytes()));
//getting the target element
NodeList list=document.getElementsByTagName("Employee");
Node node=list.item(0);    

//writing the element in the string variable
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(buffer));
String str = buffer.toString();


System.out.println(str);

...

Inspired by this thread

Community
  • 1
  • 1
FUGAZI
  • 231
  • 1
  • 4
  • 10