How do I load an org.w3c.dom.Document from XML in a string?
This works for me in Java 1.5 - I stripped out specific exceptions for readability.
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import java.io.ByteArrayInputStream;
public Document loadXMLFromString(String xml) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(xml.getBytes()));
}
Parsing String to org.w3c.dom.Document with namespaces
As per documentaion,
NodeList getElementsByTagNameNS(String namespaceURI, String localName)
The below will work,
System.out.println(document.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Envelope").getLength());
How do I convert a org.w3c.dom.Document object to a String?
If you are ok to do transformation, you may try this.
DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFact.newDocumentBuilder();
Document doc = builder.parse(st);
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
System.out.println("XML IN String format is: \n" + writer.toString());
Converting a org.w3c.dom.Document in Java to String using Transformer
Here is Document converter which works fine for me
public String getStringFromDoc(Document doc) throws TransformerException {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, result);
writer.flush();
return writer.toString();
}
Trying to parse an org.w3c.dom.Document XML Document in Java
public static void toOrderListFromXML() throws ParserConfigurationException,
SAXException, IOException {
File fXmlFile = new File("test.xml");
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" +
doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("string");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Order number: " +
eElement.getTextContent());
}
}
}
}
Then output will be
Root element :ArrayOfstring
Current Element :string
Order number: 6540321
Current Element :string
Order number: 6540322
Current Element :string
Order number: 6540323
Current Element :string
Order number: 6540324
Current Element :string
Order number: 6540325
Related Topics
String Array Initialization in Java
What to Return If Spring MVC Controller Method Doesn't Return Value
Are Java Static Calls More or Less Expensive Than Non-Static Calls
How to Add a Timeout Value When Using Java's Runtime.Exec()
Tool to Convert Java to C# Code
Generate JPA 2 Entities from Existing Database
How to Capture Video Using Jmf, But Without Installing Jmf
How to Convert Double[] to Double[]
How to "Add" to Classpath Dynamically in Java
How to Set Jvm Parameters for Junit Unit Tests
Does Java Se 8 Have Pairs or Tuples
JPA - Returning an Auto Generated Id After Persist()
Creating Animated Gif with Imageio
How to Configure Log4J to Log Different Log Levels to Different Files for the Same Logger