| |

VerySource

 Forgot password?
 Register
Search
View: 717|Reply: 5

How to get all content of an element node in xml

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-24 08:20:01
| Show all posts |Read mode
I got a node after locating, and there are other nodes under this node. I now want to take out the xml fragment of this node, that is, the string fragment of this node in the xml document. How to do it better?
Reply

Use magic Report

0

Threads

18

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-2-16 19:30:01
| Show all posts
String fragment?
What xml parser do you use to do it.
You can parse everything out of this node and then organize it backwards into strings
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-2-16 23:30:02
| Show all posts
Do you use DOM JDOM SAX or DOM4J?
Here is the DOM4J
         try {
          SAXReader reader = new SAXReader ();
          Document document = reader.read ("Demo.txt");
          Element root = document.getRootElement ();
           for (Iterator node = root.elementIterator (); node.hasNext ();) {

               // CompanyBean a = new CompanyBean ();

          Element element = (Element) node.next ();
          Attribute attribute = (Attribute) element.attribute ("value");
          a.setBirthday (element.elementText ("birthday"));
          a.setDepartment ((String) attribute.getData ());
          a.setName (element.elementText ("name"));
          a.setSalary (element.elementText ("salary"));
          list.add (a);
        }


        } catch (Exception e) {System.out.println (e.getMessage ());}
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-8 09:45:01
| Show all posts
/**
* Read xml file
*
* @param realPath:
* web path
*/
public Document procConfig(String realPath) {
realPath += cfgFile;
this.file = new File(realPath);
try {
SAXBuilder builder = new SAXBuilder();
doc = builder.build(new File(realPath));
return doc;
} catch (Exception e) {
System.err.println("Error creating XML parser in UsrConfig.java");
e.printStackTrace();
return null;
}
}

/**
* Read the elements in usrcfg.xml: the host's IP address, and port
*
* @param doc
*/
public void setUsrInfo(Document doc, String addr, String port,
String usrName, String usrPwd) {
Element root = doc.getRootElement();
Element host = root.getChild(UsrDef.TAG_HOST);
host.setAttribute(UsrDef.ATTR_ADDR, addr);
host.setAttribute(UsrDef.ATTR_PORT, port);
Element user = root.getChild(UsrDef.TAG_USER);
user.setAttribute(UsrDef.ATTR_NAME, usrName);
user.setAttribute(UsrDef.ATTR_PWD, usrPwd);
this.saveProperties();
}/**
* Read the elements in usrcfg.xml: user name, password, database data source, database user name and password.
*
* @param doc
* @throws Exception
*/
public void parseDBInfo(Document doc, String dbID, String dbUsrName,
String dbPwd) {
Element root = doc.getRootElement();
List dbList = root.getChildren(UsrDef.TAG_DB);
// System.out.println(dbList.size());
for (Iterator iter = dbList.iterator(); iter.hasNext();) {
Element db = (Element) iter.next();
String Id = db.getAttributeValue(UsrDef.ATTR_ID);
if (Id.equals(dbID)) {
db.setAttribute(UsrDef.ATTR_USERNAME, dbUsrName);
db.setAttribute(UsrDef.ATTR_PWD, dbPwd);
saveProperties();
}
if (Id.equals(dbID)) {
db.setAttribute(UsrDef.ATTR_USERNAME, dbUsrName);
db.setAttribute(UsrDef.ATTR_PWD, dbPwd);
saveProperties();
}
}
}
Reply

Use magic Report

0

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-8 11:00:01
| Show all posts
JDOM parsing
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Brazil

Post time: 2020-6-26 16:15:02
| Show all posts
The above is very good.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list