public static void main(String args[]) throws Exception { SAXBuilder builder = new SAXBuilder(); Document d = builder.build("xpath.xml"); XPath xpath = XPath.newInstance("x:collection/x:dvd"); xpath.addNamespace("x", d.getRootElement().getNamespaceURI()); System.out.println(xpath.selectNodes(d)); }
Document originalDoc = new SAXReader().read(new StringReader("<root><given></given></root>")); Element givenNode = originalDoc.getRootElement().element("given");
public JTree build(String pathToXml) throws Exception { SAXReader reader = new SAXReader(); Document doc = reader.read(pathToXml); return new JTree(build(doc.getRootElement())); } public DefaultMutableTreeNode build(Element e) { DefaultMutableTreeNode result = new DefaultMutableTreeNode(e.getText()); for(Object o : e.elements()) { Element child = (Element) o; result.add(build(child)); } return result; }
public static void main(String[] args) throws Exception { SAXBuilder builder = new SAXBuilder(); String xmlFileName = "a.xml"; Document doc = builder.build(xmlFileName); Element root = doc.getRootElement(); Element overview = root.getChild("overview"); Element content = overview.getChild("content"); XMLOutputter outp = new XMLOutputter(); outp.setFormat(Format.getCompactFormat()); //outp.setFormat(Format.getRawFormat()); //outp.setFormat(Format.getPrettyFormat()); //outp.getFormat().setTextMode(Format.TextMode.PRESERVE); StringWriter sw = new StringWriter(); outp.output(content.getContent(), sw); StringBuffer sb = sw.getBuffer(); System.out.println(sb.toString()); }
Document fichero = (Document) builder.build( xmlFile ); Element rootNode = fichero.getRootElement(); List<Element> toProcess = rootNode.getChildren( "fichada" ); for (Element tabla : toProcess) { ..... } // remove all processed nodes from the in-memory document. toProcess.clear(); // write the modified document back to disk. ....
SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new StringReader(text)); // Now examine the document, perhaps by XPath or by navigating // programmatically, like this: String fooContents = doc.getRootElement() .getChild("foo") .getText();
public class TestJdom { public static void main(String[] args) throws JDOMException, IOException { String xmlString = "<test><tag xml:lang=\"123\"></tag></test>"; SAXBuilder builder = new SAXBuilder(); StringReader stringReader = new StringReader(new String(xmlString .getBytes())); Document doc = builder.build(stringReader); List<?> attrs = doc.getRootElement().getChild("tag").getAttributes(); System.out.println(attrs); } }
// this is org.dom4j.Document, not w3c SAXReader reader = new SAXReader(); Document doc = reader.read(new File("test.xsd")); doc.getRootElement() .addElement("xs:complexType", "http://www.w3.org/2001/XMLSchema") .addAttribute("name", "Container4") .addElement("xs:sequence", "http://www.w3.org/2001/XMLSchema") .addElement("xs:element", "http://www.w3.org/2001/XMLSchema") .addAttribute("name", "element5") .addAttribute("type", "xs:string") .addAttribute("minOccurs", "0"); System.out.println(doc.asXML());
SAXBuilder parser = new SAXBuilder(); Document docConfig = parser.build("config.xml"); Element elConfig = docConfig.getRootElement(); String host = elConfig.getChildText("host");
Document doc = new XmlBuilder().build(new File("path/to/file")); Element devicesElement = doc.getRootElement(); Elements deviceElements = devicesElement.getChildElements(); for (int i = 0; i < deviceElements.size(); i++) { Element curDevice = deviceElements.get(i); .... }
FileInputStream fis = new FileInputStream(file); //where file holds the data to parse. Can also use StringBuilder SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(fis); //build a JDOM doc from file ArrayList<String> arl = new ArrayList<String>(); Element root = doc.getRootElement(); Iterator elemIter = root.getDescendants(); //you can put filters on this to only get specific types of elements ie: root.getDescendants("ZQuery"); will give you an iterator over just ZQuery elements. while (elemIter.hasNext()) { Element foo = (Element) elemIter.next(); String CDATA = foo.getChildText("ZQuery") arl.add(CDATA); }
URL url = new URL("http://localhost:8080/job/test_job/api/xml"); // read information into DOM. Document dom = new SAXReader().read(url); // scan through build list of test_job job and print its info for (Element build : (List<Element>) dom.getRootElement().elements("build")) { System.out.println(String.format("Number:%s\tUrl:%s", build.elementText("number"), build.elementText("url"))); }
SAXBuilder builder = new SAXBuilder(); Document document = (Document)builder.build(new File("somedocument.xml")); Element root = document.getRootElement(); Element d1 = root.getChild("d-1"); String txt = d1.getChildText("zora");
SAXBuilder saxBuilder = new SAXBuilder(); Document document = saxBuilder.build("url to the xml feed"); Element root = document.getRootElement(); Element iframeElement = root.getChild("iframe"); String youtubeUrl = iframeElement.getAttributeValue("src");
FileInputStream xmlFile = new FileInputStream("input.xml"); Builder builder = new Builder(false); Document doc = builder.build(xmlFile); FileOutputStream fos = new FileOutputStream("output.xml"); nu.xom.canonical.Canonicalizer outputter = new nu.xom.canonical.Canonicalizer(fos); Nodes nodes = doc.getRootElement().query("./descendant-or-self::node()|./@*"); outputter.write(nodes); fos.close();
import nu.xom.*; [...] Builder builder = new Builder(); Document alertDoc = builder.build(new File("src/xomtest", "alertset.xml")); Document weatherDoc = builder.build(new File("src/xomtest", "weatherset.xml")); Document mainDoc = builder.build("<DataSet><blank/><blank/></DataSet>", ""); Element root = mainDoc.getRootElement(); root.replaceChild( root.getFirstChildElement("blank"), alertDoc.getRootElement().copy()); root.replaceChild( root.getFirstChildElement("blank"), weatherDoc.getRootElement().copy());
SAXBuilder builder = new SAXBuilder(); Document doc; doc = builder.build(file); //rootElement would be your "taxmann" element Element rootElement = doc.getRootElement(); Element docdetailsElement = rootElement.getChild("docdetails"); Element infoElement = docdetailsElement.getChild("info"); String id = infoElement.getAttributeValue("id"); String date = infoElement.getAttributeValue("date");
Document doc = saxbuilder.build(....); Element abc = doc.getRootElement(); Element recentelement = abc.getChild("properties").getChild("Recentlyopenedfiles"); List<Element> recentfiles = recentelement.getChildren("File"); recentfiles.add(0, new Element("File").setAttribute("Path", filepath)); while (recentfiles.size() > 3) { recentfiles.remove(3); } XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat()); xout.output(doc, outputstream);
String xml = "<fakeRoot>" + xml + "</fakeRoot>"; Document doc = new Builder( false ).build( xml, null ); Nodes children = doc.getRootElement().removeChildren(); for( int ix = 0; ix < children.size(); ix++ ) { otherDocumentElement.appendChild( children.get( ix ) ); }
private String[] getRequestAttributes(String xmlString) throws SamlException { Document doc = Util.createJdomDoc(xmlString); if (doc != null) { Namespace saml = Namespace.get("urn:oasis:names:tc:SAML:2.0:assertion"); String[] samlRequestAttributes = new String[3]; Element root = doc.getRootElement(); samlRequestAttributes[0] = root.getAttributeValue("IssueInstant"); samlRequestAttributes[1] = root.getAttributeValue("ProtocolBinding"); samlRequestAttributes[2] = root.getChild("Issuer", saml).getText(); return samlRequestAttributes; } }